Skip to content
Browse files

try not to make so many funcalls

  • Loading branch information...
1 parent 67da590 commit 9bac649fa4ca6f05795e7cab8d30049aa2410cb8 @tenderlove tenderlove committed Dec 26, 2010
Showing with 6 additions and 3 deletions.
  1. +6 −3 activerecord/lib/active_record/association_preload.rb
View
9 activerecord/lib/active_record/association_preload.rb
@@ -147,13 +147,16 @@ def set_association_collection_records(id_to_record_map, reflection_name, associ
def set_association_single_records(id_to_record_map, reflection_name, associated_records, key)
seen_keys = {}
associated_records.each do |associated_record|
+ seen_key = associated_record[key].to_s
+
#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]
+ next if seen_keys.key? seen_key
+
+ seen_keys[seen_key] = true
+ mapped_records = id_to_record_map[seen_key]
mapped_records.each do |mapped_record|
association_proxy = mapped_record.send("set_#{reflection_name}_target", associated_record)
association_proxy.send(:set_inverse_instance, associated_record)

0 comments on commit 9bac649

Please sign in to comment.
Something went wrong with that request. Please try again.