Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
Created May 14, 2011 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AquaGeek/971638 to your computer and use it in GitHub Desktop.
Save AquaGeek/971638 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #2823
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index e358564..7d707e4 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -2095,7 +2095,7 @@ module ActiveRecord #:nodoc:
hash
end
- method_scoping.assert_valid_keys([ :find, :create ])
+ method_scoping.assert_valid_keys([ :find, :create, :count ])
if f = method_scoping[:find]
f.assert_valid_keys(VALID_FIND_OPTIONS)
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 727f4c1..551aa06 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -140,6 +140,13 @@ module ActiveRecord
def construct_count_options_from_args(*args)
options = {}
column_name = :all
+
+ scoped_select = if scope(:count)
+ scope(:count)[:select]
+ elsif scope(:find)
+ scope(:find)[:select]
+ end
+
# We need to handle
# count()
@@ -149,10 +156,10 @@ module ActiveRecord
# selects specified by scopes
case args.size
when 0
- column_name = scope(:find)[:select] if scope(:find)
+ column_name = scoped_select
when 1
if args[0].is_a?(Hash)
- column_name = scope(:find)[:select] if scope(:find)
+ column_name = scoped_select
options = args[0]
else
column_name = args[0]
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 75f52df..66abfc8 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -344,5 +344,11 @@ class CalculationsTest < ActiveRecord::TestCase
def test_from_option_with_table_different_than_class
assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
end
-
+
+ def test_scoped_count_takes_counter_select
+ unscoped_count = Account.count("id")
+ Account.with_scope(:find=>{:select=>"COS(id), SIN(id)"}, :count=>{:select=>"id"}) do
+ assert_equal unscoped_count, Account.count
+ end
+ end
end
From 4da1a98ea10d505adf8201d9ecfc6065507652d7 Mon Sep 17 00:00:00 2001
From: Joshua Krall <josh@transfs.com>
Date: Mon, 22 Jun 2009 02:30:52 -0500
Subject: [PATCH] Fixed problem with Model.count(:select=>'a,b')
---
activerecord/lib/active_record/calculations.rb | 3 ++-
activerecord/test/cases/calculations_test.rb | 7 ++++++-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 727f4c1..711ce0f 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -125,6 +125,7 @@ module ActiveRecord
validate_calculation_options(operation, options)
column_name = options[:select] if options[:select]
column_name = '*' if column_name == :all
+ column_name = column_name.split(/,/)[0] if column_name.is_a?(String) # with :select=>'a,b' options, only take the first col
column = column_for column_name
catch :invalid_query do
if options[:group]
@@ -187,7 +188,7 @@ module ActiveRecord
end
if options[:distinct] && column_name.to_s !~ /\s*DISTINCT\s+/i
- distinct = 'DISTINCT '
+ distinct = 'DISTINCT '
end
sql = "SELECT #{operation}(#{distinct}#{column_name}) AS #{aggregate_alias}"
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 75f52df..a246fbd 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -25,7 +25,7 @@ class CalculationsTest < ActiveRecord::TestCase
def test_should_return_nil_as_average
assert_nil NumericData.average(:bank_balance)
end
-
+
def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg')
assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg')
@@ -269,6 +269,11 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 0, Account.scoped(:select => "credit_limit").count
end
+ def test_should_count_scoped_select_with_multiple_columns
+ Account.update_all("credit_limit = NULL")
+ assert_equal 0, Account.scoped(:select => "credit_limit, COS(credit_limit) as cosine_of_credit_limit").count
+ end
+
def test_should_count_scoped_select_with_options
Account.update_all("credit_limit = NULL")
Account.last.update_attribute('credit_limit', 49)
--
1.6.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment