aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-03-07 11:49:56 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-03-07 11:49:56 -0800
commit075d0e2c3f7d5b3b121fb833fc774e41e9505962 (patch)
tree980d8bdacec18949fec94e4b8aa11ec67d8099c9 /activerecord
parent9bd5c86c3bdc70bf29be7f756d1dec2fdd4eaaf0 (diff)
parentf01f03e96390fb493830337571de578241619d4f (diff)
downloadrails-075d0e2c3f7d5b3b121fb833fc774e41e9505962.tar.gz
rails-075d0e2c3f7d5b3b121fb833fc774e41e9505962.tar.bz2
rails-075d0e2c3f7d5b3b121fb833fc774e41e9505962.zip
Merge pull request #9600 from larrylv/fix-load-target-with-identity-map-on
@target might be nil when Identity Map is enabled.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/association.rb11
1 files changed, 8 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 861df46e3c..99f307922e 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -129,8 +129,11 @@ module ActiveRecord
# 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 @@ module ActiveRecord
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?