method
associated_records_by_owner
Ruby on Rails latest stable (v7.1.3.2)
-
0 notes -
Class: ThroughAssociation
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- 7.1.3.4
- What's this?
associated_records_by_owner(preloader)
public
Hide source
# File activerecord/lib/active_record/associations/preloader/through_association.rb, line 13 def associated_records_by_owner(preloader) preloader.preload(owners, through_reflection.name, through_scope) through_records = owners.map do |owner| association = owner.association through_reflection.name center = target_records_from_association(association) [owner, Array(center)] end reset_association owners, through_reflection.name middle_records = through_records.flat_map { |(_, rec)| rec } preloaders = preloader.preload(middle_records, source_reflection.name, reflection_scope) @preloaded_records = preloaders.flat_map(&:preloaded_records) middle_to_pl = preloaders.each_with_object({}) do |pl, h| pl.owners.each { |middle| h[middle] = pl } end through_records.each_with_object({}) do |(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| rhs_records = middles.flat_map { |r| association = r.association source_reflection.name target_records_from_association(association) }.compact # Respect the order on `reflection_scope` if it exists, else use the natural order. if reflection_scope.values[:order].present? @id_map ||= id_to_index_map @preloaded_records rhs_records.sort_by { |rhs| @id_map[rhs] } else rhs_records end end end end