Browse files

Restore the destroy_by_association check in post destroy counter cache

  • Loading branch information...
1 parent 5e32f97 commit d2543412ff42d64c5fc5d763336b3f3ec9ab8eda @byroot byroot committed Apr 15, 2014
Showing with 9 additions and 2 deletions.
  1. +9 −2 activerecord/lib/active_record/counter_cache.rb
View
11 activerecord/lib/active_record/counter_cache.rb
@@ -135,7 +135,7 @@ def _create_record(*)
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 @@ def destroy_row
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

0 comments on commit d254341

Please sign in to comment.