Permalink
Browse files

Merge pull request #14756 from laurocaetano/fix-count-on-association-…

…relation

Fix count on association relation (calls to empty and size).
  • Loading branch information...
1 parent 39a607f commit 6047c6baa2bc1ca28f80523b445569ea6953d490 @rafaelfranca rafaelfranca committed Apr 15, 2014
@@ -9,14 +9,6 @@ def proxy_association
@association
end
- def size
- @association.size
- end
-
- def empty?
- @association.empty?
- end
-
def ==(other)
other == to_a
end
@@ -188,7 +188,7 @@ def ids
private
def has_include?(column_name)
- eager_loading? || (includes_values.present? && (column_name || references_eager_loaded_tables?))
+ eager_loading? || (includes_values.present? && ((column_name && column_name != :all) || references_eager_loaded_tables?))
end
def perform_calculation(operation, column_name, options = {})
@@ -840,6 +840,17 @@ def test_count
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

0 comments on commit 6047c6b

Please sign in to comment.