diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 4 |
2 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index c3e7313f6d..490c8f4712 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -169,7 +169,10 @@ module ActiveRecord end end - sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}" + if options[:distinct] && column_name.to_s !~ /\s*DISTINCT\s+/i + distinct = 'DISTINCT ' + end + sql = "SELECT #{operation}(#{distinct}#{column_name}) AS #{aggregate_alias}" # A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT. sql = "SELECT COUNT(*) AS #{aggregate_alias}" if use_workaround diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 1771065a66..0ed40b469d 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -241,6 +241,10 @@ class CalculationsTest < ActiveRecord::TestCase assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) end + def test_should_count_manual_select_with_include + assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm) + end + def test_count_with_column_parameter assert_equal 5, Account.count(:firm_id) end |