diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-06 22:22:07 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-05-06 22:44:01 -0300 |
commit | f8c4a31e0b35576c49cad16c12ebae270cdb0690 (patch) | |
tree | e6a00b2da92996b90bfb433eb7362da9c129072b /activerecord | |
parent | 0b35a3aacc384a9552832fdf0e5a0f71393ef2d1 (diff) | |
download | rails-f8c4a31e0b35576c49cad16c12ebae270cdb0690.tar.gz rails-f8c4a31e0b35576c49cad16c12ebae270cdb0690.tar.bz2 rails-f8c4a31e0b35576c49cad16c12ebae270cdb0690.zip |
Merge pull request #10489 from greenriver/ar_counter_cache_multiple_destroy
Confirm a record has not already been destroyed before decrementing counter cache
Conflicts:
activerecord/CHANGELOG.md
Conflicts:
activerecord/CHANGELOG.md
activerecord/lib/active_record/associations/builder/belongs_to.rb
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/associations/belongs_to_associations_test.rb | 20 |
3 files changed, 28 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index bf6a26b1be..c372f94729 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,5 +1,9 @@ ## unreleased ## +* Confirm a record has not already been destroyed before decrementing counter cache. + + *Ben Tucker* + * Default values for PostgreSQL bigint types now get parsed and dumped to the schema correctly. Backport #10098. diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 1759a41d93..d78717704c 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -34,7 +34,10 @@ module ActiveRecord::Associations::Builder method_name = "belongs_to_counter_cache_before_destroy_for_#{name}" mixin.redefine_method(method_name) do record = send(name) - record.class.decrement_counter(cache_column, record.id) unless record.nil? + + if record && !self.destroyed? + record.class.decrement_counter(cache_column, record.id) + end end model.before_destroy(method_name) diff --git a/activerecord/test/cases/associations/belongs_to_associations_test.rb b/activerecord/test/cases/associations/belongs_to_associations_test.rb index c9b26895ae..58c788e42d 100644 --- a/activerecord/test/cases/associations/belongs_to_associations_test.rb +++ b/activerecord/test/cases/associations/belongs_to_associations_test.rb @@ -387,6 +387,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] |