aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-09-20 20:17:49 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-09-20 20:17:49 -0700
commit6f9ea581da9381d4ce4422fe1dad1f55ee6f862a (patch)
tree2d8df63e3f678bf2825289a226927caf47fe433b /activerecord
parent99568a7a2d17f236dee0d41aa69a637f698b5275 (diff)
downloadrails-6f9ea581da9381d4ce4422fe1dad1f55ee6f862a.tar.gz
rails-6f9ea581da9381d4ce4422fe1dad1f55ee6f862a.tar.bz2
rails-6f9ea581da9381d4ce4422fe1dad1f55ee6f862a.zip
cache associated target records hash
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb28
1 files changed, 11 insertions, 17 deletions
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)