aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2014-04-15 10:54:26 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2014-04-15 10:54:26 -0300
commit5a0d8045082dbc7d0b41b83458c45c585238fc9a (patch)
treef1bb531d7fe9521fc75c42f5a23204fa6f70e3fb
parenta230530bb7564614ee852e42aca14491ecf48aff (diff)
parenta9416a407b3e5256a0a3fbe6c6ee5112ca3b6703 (diff)
downloadrails-5a0d8045082dbc7d0b41b83458c45c585238fc9a.tar.gz
rails-5a0d8045082dbc7d0b41b83458c45c585238fc9a.tar.bz2
rails-5a0d8045082dbc7d0b41b83458c45c585238fc9a.zip
Merge pull request #14756 from laurocaetano/fix-count-on-association-relation
Fix count on association relation (calls to empty and size).
-rw-r--r--activerecord/lib/active_record/association_relation.rb8
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb11
3 files changed, 12 insertions, 9 deletions
diff --git a/activerecord/lib/active_record/association_relation.rb b/activerecord/lib/active_record/association_relation.rb
index 45f1b07f69..5a84792f45 100644
--- a/activerecord/lib/active_record/association_relation.rb
+++ b/activerecord/lib/active_record/association_relation.rb
@@ -9,14 +9,6 @@ module ActiveRecord
@association
end
- def size
- @association.size
- end
-
- def empty?
- @association.empty?
- end
-
def ==(other)
other == to_a
end
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index 812e3e800a..514ebc2bfe 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -188,7 +188,7 @@ module ActiveRecord
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 = {})
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