<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -65,7 +65,13 @@ module ActiveRecord
       end
 
       def set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
+        seen_keys = {}
         associated_records.each do |associated_record|
+          #this is a has_one or belongs_to: there should only be one record.
+          #Unfortunately we can't (in portable way) ask the database for 'all records where foo_id in (x,y,z), but please
+          # only one row per distinct foo_id' so this where we enforce that
+          next if seen_keys[associated_record[key].to_s]
+          seen_keys[associated_record[key].to_s] = true
           mapped_records = id_to_record_map[associated_record[key].to_s]
           mapped_records.each do |mapped_record|
             mapped_record.send(&quot;set_#{reflection_name}_target&quot;, associated_record)
@@ -122,7 +128,6 @@ module ActiveRecord
         else
           records.each {|record| record.send(&quot;set_#{reflection.name}_target&quot;, nil)}
 
-
           set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
         end
       end</diff>
      <filename>activerecord/lib/active_record/association_preload.rb</filename>
    </modified>
    <modified>
      <diff>@@ -29,6 +29,10 @@ class EagerAssociationTest &lt; ActiveRecord::TestCase
     post = Post.find(:first, :include =&gt; :comments, :conditions =&gt; &quot;posts.title = 'Welcome to the weblog'&quot;)
     assert_equal 2, post.comments.size
     assert post.comments.include?(comments(:greetings))
+
+    posts = Post.find(:all, :include =&gt; :last_comment)
+    post = posts.find { |p| p.id == 1 }
+    assert_equal Post.find(1).last_comment, post.last_comment
   end
 
   def test_loading_conditions_with_or</diff>
      <filename>activerecord/test/cases/associations/eager_test.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,6 +9,8 @@ class Post &lt; ActiveRecord::Base
 
   belongs_to :author_with_posts, :class_name =&gt; &quot;Author&quot;, :foreign_key =&gt; :author_id, :include =&gt; :posts
 
+  has_one :last_comment, :class_name =&gt; 'Comment', :order =&gt; 'id desc'
+
   has_many   :comments, :order =&gt; &quot;body&quot; do
     def find_most_recent
       find(:first, :order =&gt; &quot;id DESC&quot;)</diff>
      <filename>activerecord/test/models/post.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>a08004a9a7d4c93c39f9693f6406ecb70d6a38c0</id>
    </parent>
  </parents>
  <author>
    <name>Frederick Cheung</name>
    <email>frederick.cheung@gmail.com</email>
  </author>
  <url>http://github.com/rails/rails/commit/fbebdb0c091c37b0bc75ab774d187d8bc8795bd2</url>
  <id>fbebdb0c091c37b0bc75ab774d187d8bc8795bd2</id>
  <committed-date>2008-05-06T02:08:52-07:00</committed-date>
  <authored-date>2008-05-01T16:00:42-07:00</authored-date>
  <message>Ensure correct record is returned when preloading has_one where more than one row exists

Signed-off-by: Michael Koziarski &lt;michael@koziarski.com&gt;
[#73 state:closed]</message>
  <tree>ce0267cb79b2f960b71a336598b14ee828b55460</tree>
  <committer>
    <name>Michael Koziarski</name>
    <email>michael@koziarski.com</email>
  </committer>
</commit>
