提交 3029a76a 编写于 作者: R Rick Olson

Fix #count on a has_many :through association so that it recognizes the :uniq...

Fix #count on a has_many :through association so that it recognizes the :uniq option.  Closes #8801 [lifofifo]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7199 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 9014bf3f
*SVN*
* Fix #count on a has_many :through association so that it recognizes the :uniq option. Closes #8801 [lifofifo]
* Fix and properly document/test count(column_name) usage. Closes #8999 [lifofifo]
* Remove deprecated count(conditions=nil, joins=nil) usage. Closes #8993 [lifofifo]
......
......@@ -100,6 +100,16 @@ def create!(attrs = nil)
def sum(*args, &block)
calculate(:sum, *args, &block)
end
def count(*args)
column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args)
if @reflection.options[:uniq]
# This is needed becase 'SELECT count(DISTINCT *)..' is not valid sql statement.
column_name = "#{@reflection.klass.table_name}.#{@reflection.klass.primary_key}" if column_name == :all
options.merge!(:distinct => true)
end
@reflection.klass.send(:with_scope, construct_scope) { @reflection.klass.count(column_name, options) }
end
protected
def method_missing(method, *args, &block)
......
......@@ -30,7 +30,16 @@ def test_has_many_uniq_through_join_model
assert_equal 2, authors(:mary).categorized_posts.size
assert_equal 1, authors(:mary).unique_categorized_posts.size
end
def test_has_many_uniq_through_count
author = authors(:mary)
assert !authors(:mary).unique_categorized_posts.loaded?
assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count }
assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count(:title) }
assert_queries(1) { assert_equal 0, author.unique_categorized_posts.count(:title, :conditions => "title is NULL") }
assert !authors(:mary).unique_categorized_posts.loaded?
end
def test_polymorphic_has_many
assert posts(:welcome).taggings.include?(taggings(:welcome_general))
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册