diff options
author | Ben Tucker <ben@btucker.net> | 2013-05-06 18:31:20 -0400 |
---|---|---|
committer | Ben Tucker <ben@btucker.net> | 2013-05-06 21:09:14 -0400 |
commit | 228720ef19e7dcf7c21f4ef2171906cc7c8c97f1 (patch) | |
tree | b390079c22358e1f2642792be93fb0c6d8cdb771 /activerecord/lib | |
parent | f7f8b7ccfc2fe61c0834041d2e211dd9dfdfbbc8 (diff) | |
download | rails-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/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 4 |
1 files changed, 3 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 543a0247d1..63e9526436 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -34,7 +34,9 @@ module ActiveRecord::Associations::Builder def belongs_to_counter_cache_before_destroy_for_#{name} unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == #{foreign_key.to_sym.inspect} record = #{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 end |