aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Verhasselt <david@crowdway.com>2014-05-23 13:46:03 +0300
committerDavid Verhasselt <david@crowdway.com>2014-06-10 11:18:52 +0300
commit63fd88ca5c255a84b61ce81c41dfe9928ea101d9 (patch)
tree88d1e1b8bbae64312aea3b83f02f2426e906624f
parent90fb179cb3fb6f1decd7e42227cd8e0251832d7b (diff)
downloadrails-63fd88ca5c255a84b61ce81c41dfe9928ea101d9.tar.gz
rails-63fd88ca5c255a84b61ce81c41dfe9928ea101d9.tar.bz2
rails-63fd88ca5c255a84b61ce81c41dfe9928ea101d9.zip
If a counter_cache exists, use it for #empty?
-rw-r--r--activerecord/lib/active_record/associations/has_many_association.rb8
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb7
2 files changed, 15 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/has_many_association.rb b/activerecord/lib/active_record/associations/has_many_association.rb
index 2727e23870..3ffc015975 100644
--- a/activerecord/lib/active_record/associations/has_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_many_association.rb
@@ -41,6 +41,14 @@ module ActiveRecord
end
end
+ def empty?
+ if has_cached_counter?
+ size.zero?
+ else
+ super
+ end
+ end
+
private
# Returns the number of records in this collection.
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 5f01352ab4..a80be99720 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -807,6 +807,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_calling_empty_with_counter_cache
+ post = posts(:welcome)
+ assert_queries(0) do
+ assert_not post.comments.empty?
+ end
+ end
+
def test_custom_named_counter_cache
topic = topics(:first)