aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/association_preload.rb
diff options
context:
space:
mode:
authorFrederick Cheung <frederick.cheung@gmail.com>2008-05-02 00:00:42 +0100
committerMichael Koziarski <michael@koziarski.com>2008-05-06 21:08:52 +1200
commitfbebdb0c091c37b0bc75ab774d187d8bc8795bd2 (patch)
treece0267cb79b2f960b71a336598b14ee828b55460 /activerecord/lib/active_record/association_preload.rb
parenta08004a9a7d4c93c39f9693f6406ecb70d6a38c0 (diff)
downloadrails-fbebdb0c091c37b0bc75ab774d187d8bc8795bd2.tar.gz
rails-fbebdb0c091c37b0bc75ab774d187d8bc8795bd2.tar.bz2
rails-fbebdb0c091c37b0bc75ab774d187d8bc8795bd2.zip
Ensure correct record is returned when preloading has_one where more than one row exists
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#73 state:closed]
Diffstat (limited to 'activerecord/lib/active_record/association_preload.rb')
-rw-r--r--activerecord/lib/active_record/association_preload.rb7
1 files changed, 6 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index 3e7c787dee..da4ebdef51 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -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("set_#{reflection_name}_target", associated_record)
@@ -122,7 +128,6 @@ module ActiveRecord
else
records.each {|record| record.send("set_#{reflection.name}_target", nil)}
-
set_association_single_records(id_to_record_map, reflection.name, find_associated_records(ids, reflection, preload_options), reflection.primary_key_name)
end
end