aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/associations/builder/belongs_to.rb16
-rw-r--r--activerecord/lib/active_record/counter_cache.rb8
2 files changed, 4 insertions, 20 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb
index bd2ee1a929..7614fdd97f 100644
--- a/activerecord/lib/active_record/associations/builder/belongs_to.rb
+++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb
@@ -30,18 +30,6 @@ module ActiveRecord::Associations::Builder
mixin.class_eval do
- 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.actually_destroyed?
- cache_column = reflection.counter_cache_column
- record.class.decrement_counter(cache_column, record.id)
- self.clear_destroy_state
- end
- end
- end
-
def belongs_to_counter_cache_after_update(reflection)
foreign_key = reflection.foreign_key
cache_column = reflection.counter_cache_column
@@ -67,10 +55,6 @@ module ActiveRecord::Associations::Builder
def self.add_counter_cache_callbacks(model, reflection)
cache_column = reflection.counter_cache_column
- model.after_destroy lambda { |record|
- record.belongs_to_counter_cache_after_destroy(reflection)
- }
-
model.after_update lambda { |record|
record.belongs_to_counter_cache_after_update(reflection)
}
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 163da8e870..1872d0c141 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -134,9 +134,7 @@ module ActiveRecord
def _create_record(*)
id = super
- each_counter_cached_associations do |association|
- association.increment_counters
- end
+ each_counter_cached_associations(&:increment_counters)
id
end
@@ -144,7 +142,9 @@ module ActiveRecord
def destroy_row
affected_rows = super
- @_actually_destroyed = affected_rows > 0
+ if affected_rows > 0
+ each_counter_cached_associations(&:decrement_counters)
+ end
affected_rows
end