diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 14:02:05 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-12-12 14:33:01 -0800 |
commit | 6772d5e74aaa3d35e91a4f5b6e46831c03315093 (patch) | |
tree | fe42ac34ad6b7c2a387d26c05397195efad65504 /activerecord | |
parent | a176bd095c3cba498369870681695bb29a68f0f9 (diff) | |
download | rails-6772d5e74aaa3d35e91a4f5b6e46831c03315093.tar.gz rails-6772d5e74aaa3d35e91a4f5b6e46831c03315093.tar.bz2 rails-6772d5e74aaa3d35e91a4f5b6e46831c03315093.zip |
pull a nil check up one frame
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_association.rb | 16 |
1 files 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 @@ module ActiveRecord 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 @@ module ActiveRecord # 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) |