From 99568a7a2d17f236dee0d41aa69a637f698b5275 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 20 Sep 2013 20:14:52 -0700 Subject: combine methods so we can reuse preloaders --- .../associations/preloader/through_association.rb | 37 +++++++++++----------- 1 file changed, 18 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 d6972bd2be..eaceb6c3d9 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -12,7 +12,24 @@ module ActiveRecord end def associated_records_by_owner - through_records = through_records_by_owner + left_loader = Preloader.new(owners, + through_reflection.name, + through_scope) + left_loader.run + + should_reset = (through_scope != through_reflection.klass.unscoped) || + (reflection.options[:source_type] && through_reflection.collection?) + + through_records = owners.map do |owner, h| + association = owner.association through_reflection.name + + x = [owner, Array(association.reader), association] + + # Dont cache the association - we would only be caching a subset + association.reset if should_reset + + x + end middle_records = through_records.map { |rec| rec[1] }.flatten @@ -30,24 +47,6 @@ module ActiveRecord private - def through_records_by_owner - Preloader.new(owners, through_reflection.name, through_scope).run - - should_reset = (through_scope != through_reflection.klass.unscoped) || - (reflection.options[:source_type] && through_reflection.collection?) - - owners.map do |owner, h| - association = owner.association through_reflection.name - - 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 - def through_scope scope = through_reflection.klass.unscoped -- cgit v1.2.3