diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-14 10:16:27 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-14 10:16:27 -0700 |
commit | a1e2db2e9bb4ca2fdf6190aa8f448fe85cf76529 (patch) | |
tree | 15cae59a1040462eab651c52e30c5b25f59aed7a /activerecord/lib/active_record/associations | |
parent | 3bf8f8b05557c5661c9816f106c582fed1f10754 (diff) | |
parent | 655a3667aa99ae3f7e68024b3971b5783d6396bf (diff) | |
download | rails-a1e2db2e9bb4ca2fdf6190aa8f448fe85cf76529.tar.gz rails-a1e2db2e9bb4ca2fdf6190aa8f448fe85cf76529.tar.bz2 rails-a1e2db2e9bb4ca2fdf6190aa8f448fe85cf76529.zip |
Merge pull request #14735 from byroot/idempotent-counter-caches
Idempotent counter caches, fix concurrency issues with counter caches
Diffstat (limited to 'activerecord/lib/active_record/associations')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index 5ccaa55a32..11be92ae01 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -37,13 +37,14 @@ module ActiveRecord::Associations::Builder end end - def belongs_to_counter_cache_before_destroy(reflection) + def belongs_to_counter_cache_after_destroy(reflection) foreign_key = reflection.foreign_key.to_sym unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key record = send reflection.name - if record && !self.destroyed? + if record && self.actually_destroyed? cache_column = reflection.counter_cache_column record.class.decrement_counter(cache_column, record.id) + self.clear_destroy_state end end end @@ -77,8 +78,8 @@ module ActiveRecord::Associations::Builder record.belongs_to_counter_cache_after_create(reflection) } - model.before_destroy lambda { |record| - record.belongs_to_counter_cache_before_destroy(reflection) + model.after_destroy lambda { |record| + record.belongs_to_counter_cache_after_destroy(reflection) } model.after_update lambda { |record| |