Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 5a0d8045082dbc7d0b41b83458c45c585238fc9a 2 parents a230530 + a9416a4
@rafaelfranca rafaelfranca authored
View
8 activerecord/lib/active_record/association_relation.rb
@@ -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
View
2  activerecord/lib/active_record/relation/calculations.rb
@@ -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 = {})
View
11 activerecord/test/cases/relations_test.rb
@@ -864,6 +864,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
Please sign in to comment.
Something went wrong with that request. Please try again.