Skip to content
Browse files

Merge pull request #9600 from larrylv/fix-load-target-with-identity-m…

…ap-on


@target might be nil when Identity Map is enabled.
  • Loading branch information...
2 parents 9bd5c86 + f01f03e commit 075d0e2c3f7d5b3b121fb833fc774e41e9505962 @rafaelfranca rafaelfranca committed Mar 7, 2013
Showing with 8 additions and 3 deletions.
  1. +8 −3 activerecord/lib/active_record/associations/association.rb
View
11 activerecord/lib/active_record/associations/association.rb
@@ -129,8 +129,11 @@ def target_scope
# This method is abstract in the sense that it relies on +find_target+,
# which is expected to be provided by descendants.
#
- # If the \target is already \loaded it is just returned. Thus, you can call
- # +load_target+ unconditionally to get the \target.
+ # If the \target is stale(the target no longer points to the record(s) that the
+ # relevant foreign_key(s) refers to.), force reload the \target.
+ #
+ # Otherwise if the \target is already \loaded it is just returned. Thus, you can
+ # call +load_target+ unconditionally to get the \target.
#
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
@@ -139,11 +142,13 @@ def load_target
begin
if IdentityMap.enabled? && association_class && association_class.respond_to?(:base_class)
@target = IdentityMap.get(association_class, owner[reflection.foreign_key])
- else
+ elsif @stale_state && stale_target?
@target = find_target
end
rescue NameError
nil
+ ensure
+ @target ||= find_target
end
end
loaded! unless loaded?

0 comments on commit 075d0e2

Please sign in to comment.
Something went wrong with that request. Please try again.