diff options
author | Emilio Tagua <miloops@gmail.com> | 2010-09-15 15:38:38 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2010-11-19 19:08:09 -0300 |
commit | 234bbe5cb0e31b786cc73e8f63e55a9ce8aecf88 (patch) | |
tree | d8835097cfe5c4d63eadf35ddfabd612ce101b18 /activerecord | |
parent | 6b0b95f1bd92e2ef35573cb59e8a14bd3ffb3777 (diff) | |
download | rails-234bbe5cb0e31b786cc73e8f63e55a9ce8aecf88.tar.gz rails-234bbe5cb0e31b786cc73e8f63e55a9ce8aecf88.tar.bz2 rails-234bbe5cb0e31b786cc73e8f63e55a9ce8aecf88.zip |
Associated objects are assigned from identity map if enabled and contains associated object.
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 |