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:23:35 -0700 2008
commit  a445cdd8840c4e99c40c6d5b15ab380d39a56be3
tree    1d9e7ee98e9607fd635f1743fa6c6bf507ba297e
parent  2dbda11945507a0541d61d13b8c564121c1cd362
...
1893
1894
1895
 
1896
1897
1898
...
1893
1894
1895
1896
1897
1898
1899
0
@@ -1893,6 +1893,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