aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/has_many_associations_test.rb
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2008-12-02 14:12:09 -0300
committerJeremy Kemper <jeremy@bitsweat.net>2008-12-10 10:59:52 -0800
commit05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc (patch)
tree6286202b3884a8b20be2f027ce76a49642143cc9 /activerecord/test/cases/associations/has_many_associations_test.rb
parent355f41d8aafd75d76db25cdda4736e0052b0605c (diff)
downloadrails-05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc.tar.gz
rails-05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc.tar.bz2
rails-05f2183747c8e75c9e8bbaadb9573b4bdf41ecfc.zip
Fix: counter_cache should decrement on deleting associated records.
[#1195 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Diffstat (limited to 'activerecord/test/cases/associations/has_many_associations_test.rb')
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 816ceb6855..1f8b297e81 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -552,6 +552,18 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
end
+ def test_deleting_updates_counter_cache
+ post = Post.first
+
+ post.comments.delete(post.comments.first)
+ post.reload
+ assert_equal post.comments(true).size, post.comments_count
+
+ post.comments.delete(post.comments.first)
+ post.reload
+ assert_equal 0, post.comments_count
+ end
+
def test_deleting_before_save
new_firm = Firm.new("name" => "A New Firm, Inc.")
new_client = new_firm.clients_of_firm.build("name" => "Another Client")
@@ -605,6 +617,14 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
end
end
+ def test_clearing_updates_counter_cache
+ post = Post.first
+
+ post.comments.clear
+ post.reload
+ assert_equal 0, post.comments_count
+ end
+
def test_clearing_a_dependent_association_collection
firm = companies(:first_firm)
client_id = firm.dependent_clients_of_firm.first.id