From a901433ee3bd318fd6c87b939ddabe0925830bc9 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 20 Sep 2013 20:11:45 -0700 Subject: [PATCH] return a list rather than hash --- .../preloader/through_association.rb | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index 3001177d15..d6972bd2be 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -14,15 +14,18 @@ def source_reflection def associated_records_by_owner through_records = through_records_by_owner - preloader = Preloader.new(through_records.values.flatten, + middle_records = through_records.map { |rec| rec[1] }.flatten + + preloader = Preloader.new(middle_records, source_reflection.name, reflection_scope) preloader.run - through_records.each do |owner, records| - records.map! { |r| r.send(source_reflection.name) }.flatten! - records.compact! - end + through_records.each_with_object({}) { |(lhs,middles,assoc),h| + h[lhs] = middles.flat_map { |r| + r.send(source_reflection.name) + }.compact + } end private @@ -33,12 +36,15 @@ def through_records_by_owner should_reset = (through_scope != through_reflection.klass.unscoped) || (reflection.options[:source_type] && through_reflection.collection?) - owners.each_with_object({}) do |owner, h| + owners.map do |owner, h| association = owner.association through_reflection.name - h[owner] = Array(association.reader) + + x = [owner, Array(association.reader), association] # Dont cache the association - we would only be caching a subset association.reset if should_reset + + x end end -- GitLab