From 57112fb7e7cb72584010b0957dfce91e0fc80a12 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <aaron.patterson@gmail.com>
Date: Wed, 25 Sep 2013 10:40:50 -0700
Subject: always populate the preloaded records instance variable so we can
 remove the @associated_records_by_owner ivar

---
 .../active_record/associations/preloader/has_many_through.rb  |  4 +---
 .../associations/preloader/through_association.rb             | 11 ++++-------
 2 files changed, 5 insertions(+), 10 deletions(-)

(limited to 'activerecord/lib')

diff --git a/activerecord/lib/active_record/associations/preloader/has_many_through.rb b/activerecord/lib/active_record/associations/preloader/has_many_through.rb
index 341e73f127..7b37b5942d 100644
--- a/activerecord/lib/active_record/associations/preloader/has_many_through.rb
+++ b/activerecord/lib/active_record/associations/preloader/has_many_through.rb
@@ -5,15 +5,13 @@ module ActiveRecord
         include ThroughAssociation
 
         def associated_records_by_owner(preloader)
-          return @associated_records_by_owner if @associated_records_by_owner
-
           records_by_owner = super
 
           if reflection_scope.distinct_value
             records_by_owner.each_value { |records| records.uniq! }
           end
 
-          @associated_records_by_owner = records_by_owner
+          records_by_owner
         end
       end
     end
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index ec67a52885..08683d3250 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -4,7 +4,6 @@ module ActiveRecord
       module ThroughAssociation #:nodoc:
         def initialize(klass, owners, reflection, preload_scope)
           super
-          @associated_records_by_owner = nil
         end
 
         def through_reflection
@@ -15,10 +14,6 @@ module ActiveRecord
           reflection.source_reflection
         end
 
-        def preloaded_records
-          @associated_records_by_owner.values.flatten
-        end
-
         def associated_records_by_owner(preloader)
           preloader.preload(owners,
                             through_reflection.name,
@@ -50,7 +45,7 @@ module ActiveRecord
             }
           end
 
-          @associated_records_by_owner = through_records.each_with_object({}) { |(lhs,center),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 |pl, middles|
@@ -64,7 +59,9 @@ module ActiveRecord
                 indexes[r] = i
                 i += 1
               }
-              rhs_records.sort_by { |rhs| record_index[rhs] }
+              records = rhs_records.sort_by { |rhs| record_index[rhs] }
+              @preloaded_records.concat rhs_records
+              records
             end
           }
         end
-- 
cgit v1.2.3