aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorstopdropandrew <andrew@andrew.local>2009-03-07 15:26:56 +0000
committerPratik Naik <pratiknaik@gmail.com>2009-03-07 15:26:56 +0000
commit6543426c73fa9ccf3649d7cbacbbb0fda9b6a099 (patch)
treecaadecdf1536a94b49312d5f3b24bd7b0a135529 /activerecord
parentccb0a92fa2ba0bf7be50033090c3a861e6d907be (diff)
downloadrails-6543426c73fa9ccf3649d7cbacbbb0fda9b6a099.tar.gz
rails-6543426c73fa9ccf3649d7cbacbbb0fda9b6a099.tar.bz2
rails-6543426c73fa9ccf3649d7cbacbbb0fda9b6a099.zip
Ensure calculations respect scoped :select [#1334 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/calculations.rb18
-rw-r--r--activerecord/test/cases/calculations_test.rb11
2 files changed, 24 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 7af97d7296..f077818d3b 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -141,22 +141,30 @@ module ActiveRecord
def construct_count_options_from_args(*args)
options = {}
column_name = :all
-
+
# We need to handle
# count()
# count(:column_name=:all)
# count(options={})
# count(column_name=:all, options={})
+ # selects specified by scopes
case args.size
+ when 0
+ column_name = scope(:find)[:select] if scope(:find)
when 1
- args[0].is_a?(Hash) ? options = args[0] : column_name = args[0]
+ if args[0].is_a?(Hash)
+ column_name = scope(:find)[:select] if scope(:find)
+ options = args[0]
+ else
+ column_name = args[0]
+ end
when 2
column_name, options = args
else
raise ArgumentError, "Unexpected parameters passed to count(): #{args.inspect}"
- end if args.size > 0
-
- [column_name, options]
+ end
+
+ [column_name || :all, options]
end
def construct_calculation_sql(operation, column_name, options) #:nodoc:
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index c158706645..0257fe57cb 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -264,6 +264,17 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
end
+ def test_should_count_scoped_select
+ Account.update_all("credit_limit = 50")
+ assert_equal 1, Account.scoped(:select => "DISTINCT credit_limit").count
+ end
+
+ def test_should_count_scoped_select_with_options
+ Account.update_all("credit_limit = 50")
+ Account.first.update_attribute('credit_limit', 49)
+ assert_equal 1, Account.scoped(:select => "DISTINCT credit_limit").count(:conditions => [ 'credit_limit >= 50'] )
+ end
+
def test_should_count_manual_select_with_include
assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
end