diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index ed0d4aef7f942..d48ff92b6c683 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -73,6 +73,8 @@ def update_counters(id, counters) "#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}" end + IdentityMap.remove_by_id(symbolized_base_class, id) if IdentityMap.enabled? + update_all(updates.join(', '), primary_key => id ) end diff --git a/activerecord/lib/active_record/identity_map.rb b/activerecord/lib/active_record/identity_map.rb index 30a2a1eb0f9da..7eb792fec4b66 100644 --- a/activerecord/lib/active_record/identity_map.rb +++ b/activerecord/lib/active_record/identity_map.rb @@ -59,6 +59,10 @@ def remove(record) repository[record.class.symbolized_base_class].delete(record.id) end + def remove_by_id(symbolized_base_class, id) + repository[symbolized_base_class].delete(id) + end + def clear repository.clear end