diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-03-31 12:55:49 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-03-31 12:56:09 +0100 |
commit | 5562abb4e985dc6f57b0643fc655e93e495602da (patch) | |
tree | 7294cb82858892ccc61d51489c7b813ea7874fab | |
parent | 607f945b1dc42bc7df85063bd6b1118759c0d8a4 (diff) | |
download | rails-5562abb4e985dc6f57b0643fc655e93e495602da.tar.gz rails-5562abb4e985dc6f57b0643fc655e93e495602da.tar.bz2 rails-5562abb4e985dc6f57b0643fc655e93e495602da.zip |
Dont try to load the record from the db if preloading didn't find anything
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/associations/eager_test.rb | 6 |
2 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index c94b2ff564..95bbaf00cf 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -159,6 +159,11 @@ module ActiveRecord association_proxy.__send__(:set_inverse_instance, associated_record, mapped_record) end end + + id_to_record_map.each do |id, records| + next if seen_keys.include?(id.to_s) + records.each {|record| record.send("set_#{reflection_name}_target", nil) } + end end # Given a collection of ActiveRecord objects, constructs a Hash which maps diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 42a891bc3b..79e5ecf4ce 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -833,4 +833,10 @@ class EagerAssociationTest < ActiveRecord::TestCase end end + def test_preloading_empty_polymorphic_parent + t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general)) + + assert_queries(2) { @tagging = Tagging.preload(:taggable).find(t.id) } + assert_no_queries { assert ! @tagging.taggable } + end end |