Merge pull request #15266 from dv/use_counter_cache_for_empty_call

If a counter_cache exists, use it for #empty?
This commit is contained in:
Godfrey Chan 2014-07-15 08:34:44 -07:00
commit 473b09f616
2 changed files with 15 additions and 0 deletions

@ -41,6 +41,14 @@ def insert_record(record, validate = true, raise = false)
end
end
def empty?
if has_cached_counter?
size.zero?
else
super
end
end
private
# Returns the number of records in this collection.

@ -837,6 +837,13 @@ def test_deleting_updates_counter_cache_with_dependent_destroy
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)