From e0fd18b2aaccbffdc52a1d05b2580b7c38219355 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Thu, 16 Aug 2007 04:58:06 +0000 Subject: [PATCH] Merge [7235] to stable. References #9167 [danger] git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-stable@7327 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ .../lib/active_record/associations/has_many_association.rb | 2 +- activerecord/test/associations/eager_test.rb | 7 +++++++ activerecord/test/fixtures/author.rb | 1 + 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 3adabd8e17d5f..c5f40dc1f6ce8 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Make sure has_many uses :include when counting [danger] + * Save associated records only if the association is already loaded. #8713 [blaine] * Changing the :default Date format doesn't break date quoting. #6312 [bshand, Elias] diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb index 0a275ab4309ce..6e12a66db8a5b 100644 --- a/activerecord/lib/active_record/associations/has_many_association.rb +++ b/activerecord/lib/active_record/associations/has_many_association.rb @@ -138,7 +138,7 @@ def count_records elsif @reflection.options[:counter_sql] @reflection.klass.count_by_sql(@counter_sql) else - @reflection.klass.count(:conditions => @counter_sql) + @reflection.klass.count(:conditions => @counter_sql, :include => @reflection.options[:include]) end @target = [] and loaded if count == 0 diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb index 0d69af1c10cff..ced95fb02e1ce 100644 --- a/activerecord/test/associations/eager_test.rb +++ b/activerecord/test/associations/eager_test.rb @@ -271,6 +271,13 @@ def test_eager_with_has_one_dependent_does_not_destroy_dependent assert_not_nil f.account assert_equal companies(:first_firm, :reload).account, f.account end + + def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size + author = authors(:david) + posts_with_no_comments = author.posts.select { |post| post.comments.blank? } + assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size + assert_equal posts_with_no_comments, author.posts_with_no_comments + end def test_eager_with_invalid_association_reference assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") { diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 6b34c7522c2e3..5571f2cf1eac8 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -25,6 +25,7 @@ def testing_proxy_target has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'" has_many :hello_post_comments, :through => :hello_posts, :source => :comments + has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments has_many :other_posts, :class_name => "Post" has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,