From 4bc9bacd9458af9f9ad3a5075de6425e3c1c6a1e Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 18 Jan 2011 15:05:18 -0800 Subject: refactor elaborate group_by in to a normal group_by --- activerecord/lib/active_record/association_preload.rb | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record/association_preload.rb') diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 68aaff175a..4b90845244 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -177,16 +177,11 @@ module ActiveRecord # (id_to_record_map, ids) where +id_to_record_map+ is the Hash, # and +ids+ is an Array of record IDs. def construct_id_map(records, primary_key=nil) - id_to_record_map = {} - ids = [] - records.each do |record| + id_to_record_map = records.group_by do |record| primary_key ||= record.class.primary_key - ids << record[primary_key] - mapped_records = (id_to_record_map[ids.last.to_s] ||= []) - mapped_records << record + record[primary_key].to_s end - ids.uniq! - return id_to_record_map, ids + return id_to_record_map, id_to_record_map.keys end def preload_has_and_belongs_to_many_association(records, reflection, preload_options={}) -- cgit v1.2.3