From 6772d5e74aaa3d35e91a4f5b6e46831c03315093 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Thu, 12 Dec 2013 14:02:05 -0800 Subject: [PATCH] pull a nil check up one frame --- .../associations/belongs_to_association.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 7dc817fc66..2fd2cba8fc 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -40,7 +40,15 @@ def find_target? def update_counters(record) counter_cache_name = reflection.counter_cache_column - if counter_cache_name && owner.persisted? && different_target?(record) + return unless counter_cache_name && owner.persisted? + + diff_target = if record + different_target?(record) + else + owner[reflection.foreign_key] + end + + if diff_target if record record.class.increment_counter(counter_cache_name, record.id) end @@ -53,11 +61,7 @@ def update_counters(record) # Checks whether record is different to the current target, without loading it def different_target?(record) - if record.nil? - owner[reflection.foreign_key] - else - record.id != owner[reflection.foreign_key] - end + record.id != owner[reflection.foreign_key] end def replace_keys(record) -- GitLab