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