From 834e5336a5d8a8250251e756385e39ebfb4917c3 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 17 Dec 2010 20:54:50 +0000 Subject: has_many associations with :dependent => :delete_all should update the counter cache when deleting records --- .../test/cases/associations/has_many_associations_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index fb772bb8e6..2b7ad3642a 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -616,6 +616,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase end end + def test_deleting_updates_counter_cache_with_dependent_delete_all + post = posts(:welcome) + + # Manually update the count as the tagging will have been added to the taggings association, + # rather than to the taggings_with_delete_all one (which is just a 'shadow' of the former) + post.update_attribute(:taggings_with_delete_all_count, post.taggings_with_delete_all.to_a.count) + + assert_difference "post.reload.taggings_with_delete_all_count", -1 do + post.taggings_with_delete_all.delete(post.taggings_with_delete_all.first) + end + end + def test_deleting_a_collection force_signal37_to_load_all_clients_of_firm companies(:first_firm).clients_of_firm.create("name" => "Another Client") -- cgit v1.2.3