diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/counter_cache.rb | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index 4108b46439..b7b790322a 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -135,7 +135,7 @@ module ActiveRecord id = super each_counter_cached_associations do |association| - if record = send(association.reflection.name) + if send(association.reflection.name) association.increment_counters @_after_create_counter_called = true end @@ -148,7 +148,14 @@ module ActiveRecord affected_rows = super if affected_rows > 0 - each_counter_cached_associations(&:decrement_counters) + each_counter_cached_associations do |association| + foreign_key = association.reflection.foreign_key.to_sym + unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key + if send(association.reflection.name) + association.decrement_counters + end + end + end end affected_rows |