aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations/preloader/through_association.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader/through_association.rb')
-rw-r--r--activerecord/lib/active_record/associations/preloader/through_association.rb28
1 files changed, 23 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb
index fa32cc5553..b16fca7dc9 100644
--- a/activerecord/lib/active_record/associations/preloader/through_association.rb
+++ b/activerecord/lib/active_record/associations/preloader/through_association.rb
@@ -13,22 +13,38 @@ module ActiveRecord
end
def associated_records_by_owner(preloader)
+ already_loaded = owners.first.association(through_reflection.name).loaded?
through_scope = through_scope()
- preloader.preload(owners,
- through_reflection.name,
- through_scope)
+ unless already_loaded
+ preloader.preload(owners, through_reflection.name, through_scope)
+ end
through_records = owners.map do |owner|
center = owner.association(through_reflection.name).target
[owner, Array(center)]
end
- reset_association(owners, through_reflection.name, through_scope)
+ if already_loaded
+ if source_type = reflection.options[:source_type]
+ through_records.map! do |owner, center|
+ center = center.select do |record|
+ record[reflection.foreign_type] == source_type
+ end
+ [owner, center]
+ end
+ end
+ else
+ reset_association(owners, through_reflection.name, through_scope)
+ end
middle_records = through_records.flat_map(&:last)
- reflection_scope = reflection_scope() if reflection.scope
+ if preload_scope
+ reflection_scope = reflection_scope().merge(preload_scope)
+ elsif reflection.scope
+ reflection_scope = reflection_scope()
+ end
preloaders = preloader.preload(middle_records,
source_reflection.name,
@@ -58,6 +74,8 @@ module ActiveRecord
rhs_records
end
end
+ end.tap do
+ reset_association(middle_records, source_reflection.name, preload_scope)
end
end