Fixed that count distinct should use the selected column even when using...

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
上级 16716090
*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 <blair@orcaware.com>]
......
......@@ -155,7 +155,7 @@ def construct_calculation_sql(operation, column_name, options) #:nodoc:
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}"
......
......@@ -187,4 +187,9 @@ def test_should_reject_invalid_options
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册