From a9bed985cfd7d1ae93f475542bb878aa939e1c1e Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Mon, 3 Jan 2011 13:38:40 +0000 Subject: When preloading a belongs_to, the target should still be set (to nil) if there is no foreign key present. And the loaded flag should be set on the association proxy. This then allows us to remove the foreign_key_present? check from BelongsToAssociation#find_target. Also added a test for the same thing on polymorphic associations. --- activerecord/test/cases/associations/eager_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activerecord/test/cases/associations/eager_test.rb') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 2a9351507a..e11f1009dc 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -212,6 +212,15 @@ class EagerAssociationTest < ActiveRecord::TestCase end end + def test_finding_with_includes_on_null_belongs_to_polymorphic_association + sponsor = sponsors(:moustache_club_sponsor_for_groucho) + sponsor.update_attributes!(:sponsorable => nil) + sponsor = assert_queries(1) { Sponsor.find(sponsor.id, :include => :sponsorable) } + assert_no_queries do + assert_equal nil, sponsor.sponsorable + end + end + def test_loading_from_an_association posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id") assert_equal 2, posts.first.comments.size -- cgit v1.2.3