diff options
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 21 |
1 files changed, 10 insertions, 11 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 625a2efabd..8272a5584c 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -37,23 +37,22 @@ module ActiveRecord !loaded? && foreign_key_present? && klass end - def update_counters(record) + def with_cache_name counter_cache_name = reflection.counter_cache_column - return unless counter_cache_name && owner.persisted? - return unless different_target? record - - record.class.increment_counter(counter_cache_name, record.id) + yield counter_cache_name + end - decrement_counter counter_cache_name + def update_counters(record) + with_cache_name do |name| + return unless different_target? record + record.class.increment_counter(name, record.id) + decrement_counter name + end end def decrement_counters - counter_cache_name = reflection.counter_cache_column - - return unless counter_cache_name && owner.persisted? - - decrement_counter counter_cache_name + with_cache_name { |name| decrement_counter name } end def decrement_counter counter_cache_name |