From 34945e41c24c59268bbde63abfafe20bdc09b775 Mon Sep 17 00:00:00 2001 From: Lauro Caetano Date: Mon, 14 Apr 2014 21:24:31 -0300 Subject: The Association Relation should use `empty?` and `size` from Relation. 968c581ea34b5236af14805e6a77913b1cb36238 have introduced this bug #14744 on Association Relation when the method `empty?` or `size` was called. Example: # Given an author that does have 3 posts, but none of them with the # title 'Some Title' Author.last.posts.where(title: 'Some Title').size # => 3 It was occurring, because the Association Relation had implemented these methods based on `@association`, this way giving wrong results. To fix it, was necessary to remove the methods `empty?` and `size` from Association Relation. It just have to use these methods from Relation. Example: # Given an author that does have 3 posts, but none of them with the # title 'Some Title' Author.last.posts.where(title: 'Some Title').size # => 0 # Now it will return the correct value. Fixes #14744. --- activerecord/test/cases/relations_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 562cfe6796..ddfcaaf986 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -864,6 +864,17 @@ class RelationTest < ActiveRecord::TestCase assert_equal 9, posts.where(:comments_count => 0).count end + def test_count_on_association_relation + author = Author.last + another_author = Author.first + posts = Post.where(author_id: author.id) + + assert_equal author.posts.where(author_id: author.id).size, posts.count + + assert_equal 0, author.posts.where(author_id: another_author.id).size + assert author.posts.where(author_id: another_author.id).empty? + end + def test_count_with_distinct posts = Post.all -- cgit v1.2.3