From 8b9542001d5e3fefa67f15af0b303aaf1ea95875 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 22 Dec 2014 16:38:38 -0700 Subject: [PATCH] Improve the performance of reading belongs_to associations `ActiveRecord::Base#[]` has overhead that was introduced in 4.2. The `foo["id"]` working with PKs other than ID isn't really a case that we want to support publicly, but deprecating was painful enough that we avoid it. `_read_attribute` was introduced as the faster alternative for use internally. By using that, we can save a lot of overhead. We also save some overhead by reading the attribute one fewer times in `stale_state`. Fixes #18151 --- .../associations/belongs_to_association.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb index 81fdd681de..c63b42e2a0 100644 --- a/activerecord/lib/active_record/associations/belongs_to_association.rb +++ b/activerecord/lib/active_record/associations/belongs_to_association.rb @@ -73,11 +73,11 @@ module ActiveRecord # Checks whether record is different to the current target, without loading it def different_target?(record) - record.id != owner[reflection.foreign_key] + record.id != owner._read_attribute(reflection.foreign_key) end def replace_keys(record) - owner[reflection.foreign_key] = record[reflection.association_primary_key(record.class)] + owner[reflection.foreign_key] = record._read_attribute(reflection.association_primary_key(record.class)) end def remove_keys @@ -85,7 +85,7 @@ module ActiveRecord end def foreign_key_present? - owner[reflection.foreign_key] + owner._read_attribute(reflection.foreign_key) end # NOTE - for now, we're only supporting inverse setting from belongs_to back onto @@ -99,12 +99,13 @@ module ActiveRecord if options[:primary_key] owner.send(reflection.name).try(:id) else - owner[reflection.foreign_key] + owner._read_attribute(reflection.foreign_key) end end def stale_state - owner[reflection.foreign_key] && owner[reflection.foreign_key].to_s + result = owner._read_attribute(reflection.foreign_key) + result && result.to_s end end end