<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1734,6 +1734,7 @@ module ActiveRecord
                   collection.target.push(association)
                 when :has_one
                   return if record.id.to_s != join.parent.record_id(row).to_s
+                  return if record.instance_variable_defined?(&quot;@#{join.reflection.name}&quot;)
                   association = join.instantiate(row) unless row[join.aliased_primary_key].nil?
                   record.send(&quot;set_#{join.reflection.name}_target&quot;, association)
                 when :belongs_to</diff>
      <filename>activerecord/lib/active_record/associations.rb</filename>
    </modified>
    <modified>
      <diff>@@ -38,6 +38,12 @@ class EagerAssociationTest &lt; ActiveRecord::TestCase
     assert_equal Post.find(1).last_comment, post.last_comment
   end
 
+  def test_loading_with_one_association_with_non_preload
+    posts = Post.find(:all, :include =&gt; :last_comment, :order =&gt; 'comments.id DESC')
+    post = posts.find { |p| p.id == 1 }
+    assert_equal Post.find(1).last_comment, post.last_comment
+  end
+
   def test_loading_conditions_with_or
     posts = authors(:david).posts.find(:all, :include =&gt; :comments, :conditions =&gt; &quot;comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'&quot;)
     assert_nil posts.detect { |p| p.author_id != authors(:david).id },</diff>
      <filename>activerecord/test/cases/associations/eager_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -91,4 +91,14 @@ class HasOneThroughAssociationsTest &lt; ActiveRecord::TestCase
     assert_not_nil assert_no_queries {members[0].sponsor_club}
   end
 
+  def test_has_one_through_nonpreload_eager_loading_through_polymorphic_with_more_than_one_through_record
+    Sponsor.new(:sponsor_club =&gt; clubs(:crazy_club), :sponsorable =&gt; members(:groucho)).save!
+    members = assert_queries(1) do
+      Member.find(:all, :include =&gt; :sponsor_club, :conditions =&gt; [&quot;members.name = ?&quot;, &quot;Groucho Marx&quot;], :order =&gt; 'clubs.name DESC') #force fallback
+    end
+    assert_equal 1, members.size
+    assert_not_nil assert_no_queries { members[0].sponsor_club }
+    assert_equal clubs(:crazy_club), members[0].sponsor_club
+  end
+
 end</diff>
      <filename>activerecord/test/cases/associations/has_one_through_associations_test.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>bff0f5fb6d95c8f844fd6e081f827801df20114c</id>
    </parent>
  </parents>
  <author>
    <name>Tarmo T&#228;nav</name>
    <email>tarmo@itech.ee</email>
  </author>
  <url>http://github.com/rails/rails/commit/6ae0a0557d5e2859e359275b5feebb7e3c13271c</url>
  <id>6ae0a0557d5e2859e359275b5feebb7e3c13271c</id>
  <committed-date>2008-08-25T21:25:27-07:00</committed-date>
  <authored-date>2008-08-25T17:01:24-07:00</authored-date>
  <message>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 &lt;jeremy@bitsweat.net&gt;</message>
  <tree>e4390ed602a73ddf9727bdfcdaf1d5d5f63bf199</tree>
  <committer>
    <name>Jeremy Kemper</name>
    <email>jeremy@bitsweat.net</email>
  </committer>
</commit>
