From bb9554ad62a3f37738ee7ad99dbbb9a37784c343 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 23 Sep 2013 18:15:38 -0700 Subject: we can't sort by lhs since the middle records have difference classes and possibly different rules for finding those objects --- .../associations/preloader/through_association.rb | 38 +++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'activerecord/lib/active_record/associations/preloader') diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index 60f8c754f6..16a48c1db2 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -46,26 +46,26 @@ module ActiveRecord } end - @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,middles),h| - preloader = middle_to_pl[middles.first] - - rhs_records = middles.flat_map { |r| - r.send(source_reflection.name) - }.compact - - if preloader && preloader.loaded? - loaded_records = preloader.preloaded_records - i = 0 - record_index = loaded_records.each_with_object({}) { |r,indexes| - indexes[r] = i - i += 1 - } - rs = rhs_records.sort_by { |rhs| record_index[rhs] } - else - rs = rhs_records + @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,center),records_by_owner| + pl_to_middle = center.group_by { |record| middle_to_pl[record] } + + records_by_owner[lhs] = pl_to_middle.flat_map do |preloader, middles| + rhs_records = middles.flat_map { |r| + r.send(source_reflection.name) + }.compact + + if preloader && preloader.loaded? + loaded_records = preloader.preloaded_records + i = 0 + record_index = loaded_records.each_with_object({}) { |r,indexes| + indexes[r] = i + i += 1 + } + rhs_records.sort_by { |rhs| record_index[rhs] } + else + rhs_records + end end - - h[lhs] = rs } end -- cgit v1.2.3