Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Associated objects are assigned from identity map if enabled and cont…

…ains associated object.
  • Loading branch information...
commit 234bbe5cb0e31b786cc73e8f63e55a9ce8aecf88 1 parent 6b0b95f
@miloops miloops authored
View
7 activerecord/lib/active_record/associations/association_proxy.rb
@@ -252,8 +252,11 @@ def method_missing(method, *args)
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
View
18 activerecord/test/cases/identity_map_test.rb
@@ -287,4 +287,22 @@ def test_reload_object_if_update_attributes_fails
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
Please sign in to comment.
Something went wrong with that request. Please try again.