aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorBen Tucker <ben@btucker.net>2013-05-06 18:31:20 -0400
committerBen Tucker <ben@btucker.net>2013-05-06 21:09:14 -0400
commit228720ef19e7dcf7c21f4ef2171906cc7c8c97f1 (patch)
treeb390079c22358e1f2642792be93fb0c6d8cdb771 /activerecord/test
parentf7f8b7ccfc2fe61c0834041d2e211dd9dfdfbbc8 (diff)
downloadrails-228720ef19e7dcf7c21f4ef2171906cc7c8c97f1.tar.gz
rails-228720ef19e7dcf7c21f4ef2171906cc7c8c97f1.tar.bz2
rails-228720ef19e7dcf7c21f4ef2171906cc7c8c97f1.zip
Confirm a record has not already been destroyed before decrementing
counter cache At present, calling destroy multiple times on the same record results in the belongs_to counter cache being decremented multiple times. With this change the record is checked for whether it is already destroyed prior to decrementing the counter cache.
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/associations/belongs_to_associations_test.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb
index f5316952b8..87af24cbe6 100644
--- a/activerecord/test/cases/associations/belongs_to_associations_test.rb
+++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -414,6 +414,26 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
assert_equal 15, topic.replies.size
end
+ def test_counter_cache_double_destroy
+ topic = Topic.create :title => "Zoom-zoom-zoom"
+
+ 5.times do
+ topic.replies.create(:title => "re: zoom", :content => "speedy quick!")
+ end
+
+ assert_equal 5, topic.reload[:replies_count]
+ assert_equal 5, topic.replies.size
+
+ reply = topic.replies.first
+
+ reply.destroy
+ assert_equal 4, topic.reload[:replies_count]
+
+ reply.destroy
+ assert_equal 4, topic.reload[:replies_count]
+ assert_equal 4, topic.replies.size
+ end
+
def test_custom_counter_cache
reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
assert_equal 0, reply[:replies_count]