From c11102d4f80267e3774985329ea93e7368e8684f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Nov 2010 14:32:41 -0700 Subject: [PATCH] make sure we keep parent records in order --- activerecord/lib/active_record/associations.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 8d415cdfdf4..aad0cbb895c 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1873,15 +1873,15 @@ module ActiveRecord def instantiate(rows) primary_key = join_base.aliased_primary_key - base_records_hash = {} + parents = {} - rows.each do |model| + records = rows.map { |model| primary_id = model[primary_key] - base_records_hash[primary_id] ||= join_base.instantiate(model) - construct(base_records_hash[primary_id], @associations, join_associations.dup, model) - end + parent = parents[primary_id] ||= join_base.instantiate(model) + construct(parent, @associations, join_associations.dup, model) + parent + }.uniq - records = base_records_hash.values remove_duplicate_results!(join_base.active_record, records, @associations) records end