From f8c4a31e0b35576c49cad16c12ebae270cdb0690 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Mon, 6 May 2013 22:22:07 -0300 Subject: 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 --- activerecord/lib/active_record/associations/builder/belongs_to.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/associations') 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) -- cgit v1.2.3