diff options
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/associations/association_proxy.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/identity_map_test.rb | 18 |
2 files changed, 23 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 7cd04a1ad5..bfedffcf02 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -252,8 +252,11 @@ module ActiveRecord def load_target return nil unless defined?(@loaded) - if !loaded? and (@owner.persisted? || foreign_key_present) - @target = find_target + if !loaded? and (!@owner.persisted? || foreign_key_present) + if IdentityMap.enabled? + @target = IdentityMap.get(@reflection.class_name, @owner[@reflection.association_foreign_key]) + end + @target ||= find_target end @loaded = true diff --git a/activerecord/test/cases/identity_map_test.rb b/activerecord/test/cases/identity_map_test.rb index 7deda56541..e62e99361a 100644 --- a/activerecord/test/cases/identity_map_test.rb +++ b/activerecord/test/cases/identity_map_test.rb @@ -287,4 +287,22 @@ class IdentityMapTest < ActiveRecord::TestCase assert_not_equal developer.salary, same_developer.salary end + def test_owner_object_is_associated_from_identity_map + post = Post.find(1) + comment = post.comments.first + + assert_no_queries do + comment.post + end + assert_same post, comment.post + end + + def test_associated_object_are_assigned_from_identity_map + post = Post.find(1) + + post.comments.each do |comment| + assert_same post, comment.post + assert_equal post.object_id, comment.post.target.object_id + end + end end |