If a counter_cache exists, use it for #empty?

This commit is contained in:
David Verhasselt 2014-05-23 13:46:03 +03:00
parent 90fb179cb3
commit 63fd88ca5c
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.

@ -807,6 +807,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)