method

records_by_owner

Importance_0
Ruby on Rails latest stable (v7.1.3.2) - 0 notes - Class: ThroughAssociation
records_by_owner() 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 11
        def records_by_owner
          return @records_by_owner if defined?(@records_by_owner)

          @records_by_owner = owners.each_with_object({}) do |owner, result|
            if loaded?(owner)
              result[owner] = target_for(owner)
              next
            end

            through_records = through_records_by_owner[owner] || []

            if owners.first.association(through_reflection.name).loaded?
              if source_type = reflection.options[:source_type]
                through_records = through_records.select do |record|
                  record[reflection.foreign_type] == source_type
                end
              end
            end

            records = through_records.flat_map do |record|
              source_records_by_owner[record]
            end

            records.compact!
            records.sort_by! { |rhs| preload_index[rhs] } if scope.order_values.any?
            records.uniq! if scope.distinct_value
            result[owner] = records
          end
        end
Register or log in to add new notes.