From f008566d656fb3b86c6421520ffcd05828a2108f Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Thu, 16 Aug 2007 04:37:31 +0000 Subject: Don't clobber :includes passed to count on has_many association. Closes #9175 [danger] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7326 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/associations/has_many_association.rb | 2 +- activerecord/test/associations/eager_test.rb | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index a53f57b48f..1b99306c0e 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Don't clobber includes passed to has_many.count [danger] + * Make sure has_many uses :include when counting [danger] * Change the implementation of ActiveRecord's attribute reader and writer methods [nzkoz] diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index c40982d738..d385691967 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -31,7 +31,7 @@ module ActiveRecord options[:conditions] = options[:conditions].nil? ? @finder_sql : @finder_sql + " AND (#{sanitize_sql(options[:conditions])})" - options[:include] = @reflection.options[:include] + options[:include] ||= @reflection.options[:include] @reflection.klass.count(column_name, options) end diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb index 114da92542..019d361f3d 100644 --- a/activerecord/test/associations/eager_test.rb +++ b/activerecord/test/associations/eager_test.rb @@ -168,6 +168,12 @@ class EagerAssociationTest < Test::Unit::TestCase posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'") assert_equal 0, posts.size end + + def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional + author = authors(:david) + author_posts_without_comments = author.posts.select { |post| post.comments.blank? } + assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null') + end def test_eager_with_has_and_belongs_to_many_and_limit posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3) -- cgit v1.2.3