From 4394e402b7a482675ceb5ab9e832defe930bd71e Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 3 Jun 2006 21:19:36 +0000 Subject: Fixed that count distinct should use the selected column even when using :include (closes #5251) [anna@wota.jp] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4417 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/calculations.rb | 2 +- activerecord/test/calculations_test.rb | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 6f940f0d0f..86c61e51a3 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that count distinct should use the selected column even when using :include #5251 [anna@wota.jp] + * Fixed that :includes merged from with_scope won't cause the same association to be loaded more than once if repetition occurs in the clauses #5253 [alex@purefiction.net] * Allow models to override to_xml. #4989 [Blair Zajac ] diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 409466866f..e0ac96c879 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -155,7 +155,7 @@ module ActiveRecord if merged_includes.any? && operation.to_s.downcase == 'count' options[:distinct] = true - column_name = [table_name, primary_key] * '.' + column_name = options[:select] || [table_name, primary_key] * '.' end sql = "SELECT #{operation}(#{'DISTINCT ' if options[:distinct]}#{column_name}) AS #{aggregate_alias}" diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb index c4e41b1dda..785a4d10b4 100644 --- a/activerecord/test/calculations_test.rb +++ b/activerecord/test/calculations_test.rb @@ -187,4 +187,9 @@ class CalculationsTest < Test::Unit::TestCase assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) } assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) } end + + def test_should_count_selected_field_with_include + assert_equal 5, Account.count(:distinct => true, :include => :firm) + assert_equal 3, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) + end end -- cgit v1.2.3