diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/association.rb | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 2264631584..2eb431dfec 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -24,6 +24,7 @@ module ActiveRecord def initialize(owner, reflection) reflection.check_validity! + @target = nil @owner, @reflection = owner, reflection @updated = false @@ -42,6 +43,7 @@ module ActiveRecord # Resets the \loaded flag to +false+ and sets the \target to +nil+. def reset @loaded = false + IdentityMap.remove(@target) if IdentityMap.enabled? && @target @target = nil end @@ -141,7 +143,17 @@ module ActiveRecord # ActiveRecord::RecordNotFound is rescued within the method, and it is # not reraised. The proxy is \reset and +nil+ is the return value. def load_target - @target = find_target if find_target? + if find_target? + begin + if IdentityMap.enabled? && association_class && association_class.respond_to?(:base_class) + @target = IdentityMap.get(association_class, @owner[@reflection.foreign_key]) + end + rescue NameError + nil + ensure + @target ||= find_target + end + end loaded! target rescue ActiveRecord::RecordNotFound @@ -241,6 +253,10 @@ module ActiveRecord # This is only relevant to certain associations, which is why it returns nil by default. def stale_state end + + def association_class + @reflection.klass + end end end end |