method

run

Importance_0
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
  • 3.2.1
  • 3.2.8
  • 3.2.13
  • 4.0.2
  • 4.1.8
  • 4.2.1
  • 4.2.7
  • 4.2.9
  • 5.0.0.1
  • 5.1.7
  • 5.2.3 (0)
  • 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?
run(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 7
        def run(preloader)
          already_loaded     = owners.first.association(through_reflection.name).loaded?
          through_scope      = through_scope()
          reflection_scope   = target_reflection_scope
          through_preloaders = preloader.preload(owners, through_reflection.name, through_scope)
          middle_records     = through_preloaders.flat_map(&:preloaded_records)
          preloaders         = preloader.preload(middle_records, source_reflection.name, reflection_scope)
          @preloaded_records = preloaders.flat_map(&:preloaded_records)

          owners.each do |owner|
            through_records = Array(owner.association(through_reflection.name).target)
            if already_loaded
              if source_type = reflection.options[:source_type]
                through_records = through_records.select do |record|
                  record[reflection.foreign_type] == source_type
                end
              end
            else
              owner.association(through_reflection.name).reset if through_scope
            end
            result = through_records.flat_map do |record|
              association = record.association(source_reflection.name)
              target = association.target
              association.reset if preload_scope
              target
            end
            result.compact!
            if reflection_scope
              result.sort_by! { |rhs| preload_index[rhs] } if reflection_scope.order_values.any?
              result.uniq! if reflection_scope.distinct_value
            end
            associate_records_to_owner(owner, result)
          end
        end
Register or log in to add new notes.