public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Load the first and not the last has_one result when doing join-based eager 
loading

This matters when the has_one is defined with an order in which case
there is an expectation that the first one will be loaded.

[#904 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
Tarmo Tänav (author)
Mon Aug 25 17:01:24 -0700 2008
jeremy (committer)
Mon Aug 25 21:25:27 -0700 2008
commit  6ae0a0557d5e2859e359275b5feebb7e3c13271c
tree    e4390ed602a73ddf9727bdfcdaf1d5d5f63bf199
parent  bff0f5fb6d95c8f844fd6e081f827801df20114c
...
1734
1735
1736
 
1737
1738
1739
...
1734
1735
1736
1737
1738
1739
1740
0
@@ -1734,6 +1734,7 @@ module ActiveRecord
0
                   collection.target.push(association)
0
                 when :has_one
0
                   return if record.id.to_s != join.parent.record_id(row).to_s
0
+                  return if record.instance_variable_defined?("@#{join.reflection.name}")
0
                   association = join.instantiate(row) unless row[join.aliased_primary_key].nil?
0
                   record.send("set_#{join.reflection.name}_target", association)
0
                 when :belongs_to
...
38
39
40
 
 
 
 
 
 
41
42
43
...
38
39
40
41
42
43
44
45
46
47
48
49
0
@@ -38,6 +38,12 @@ class EagerAssociationTest < ActiveRecord::TestCase
0
     assert_equal Post.find(1).last_comment, post.last_comment
0
   end
0
 
0
+  def test_loading_with_one_association_with_non_preload
0
+    posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC')
0
+    post = posts.find { |p| p.id == 1 }
0
+    assert_equal Post.find(1).last_comment, post.last_comment
0
+  end
0
+
0
   def test_loading_conditions_with_or
0
     posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
0
     assert_nil posts.detect { |p| p.author_id != authors(:david).id },
...
91
92
93
 
 
 
 
 
 
 
 
 
 
94
...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
0
@@ -91,4 +91,14 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
0
     assert_not_nil assert_no_queries {members[0].sponsor_club}
0
   end
0
 
0
+  def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
0
+    Sponsor.new(:sponsor_club => clubs(:crazy_club), :sponsorable => members(:groucho)).save!
0
+    members = assert_queries(1) do
0
+      Member.find(:all, :include => :sponsor_club, :conditions => ["members.name = ?", "Groucho Marx"], :order => 'clubs.name DESC') #force fallback
0
+    end
0
+    assert_equal 1, members.size
0
+    assert_not_nil assert_no_queries { members[0].sponsor_club }
0
+    assert_equal clubs(:crazy_club), members[0].sponsor_club
0
+  end
0
+
0
 end

Comments