Flowdock
method

associated_records_by_owner

Importance_0
Ruby on Rails latest stable (v6.1.7.7) - 0 notes - Class: ThroughAssociation

Method deprecated or moved

This method is deprecated or moved on the latest stable version. The last existing version (v5.1.7) is shown here.

associated_records_by_owner(preloader) public

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

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
Register or log in to add new notes.