From 6f9ea581da9381d4ce4422fe1dad1f55ee6f862a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 20 Sep 2013 20:17:49 -0700 Subject: cache associated target records hash --- .../associations/preloader/association.rb | 28 +++++++++------------- 1 file changed, 11 insertions(+), 17 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index a8edf21bef..ef48e9f174 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -16,7 +16,7 @@ module ActiveRecord @scope = nil @owners_by_key = nil @type_caster = IDENTITY_CASTER - @target_records = nil + @associated_records_by_owner = nil end def run @@ -77,21 +77,9 @@ module ActiveRecord private - def lhs_records - return @target_records if @target_records - - owners_map = owners_by_key - owner_keys = owners_map.keys.compact - - sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size) - @target_records = sliced.flat_map { |slice| - records = records_for(slice) - set_type_caster records, association_key_name - records - } - end - def associated_records_by_owner + return @associated_records_by_owner if @associated_records_by_owner + owners_map = owners_by_key owner_keys = owners_map.keys.compact @@ -101,8 +89,14 @@ module ActiveRecord if klass && owner_keys.any? # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000) # Make several smaller queries if necessary or make one query if the adapter supports it + sliced = owner_keys.each_slice(klass.connection.in_clause_length || owner_keys.size) + records = sliced.flat_map { |slice| + records = records_for(slice) + set_type_caster records, association_key_name + records + } caster = type_caster - lhs_records.each do |record| + records.each do |record| owner_key = caster.call record[association_key_name] owners_map[owner_key].each do |owner| @@ -111,7 +105,7 @@ module ActiveRecord end end - records_by_owner + @associated_records_by_owner = records_by_owner end def set_type_caster(results, name) -- cgit v1.2.3