Method: ActiveRecord::Associations::Preloader::Branch#preloaders_for_reflection

Defined in:
activerecord/lib/active_record/associations/preloader/branch.rb

#preloaders_for_reflection(reflection, reflection_records) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'activerecord/lib/active_record/associations/preloader/branch.rb', line 91

def preloaders_for_reflection(reflection, reflection_records)
  reflection_records.group_by do |record|
    klass = record.association(association).klass

    if reflection.scope && reflection.scope.arity != 0
      # For instance dependent scopes, the scope is potentially
      # different for each record. To allow this we'll group each
      # object separately into its own preloader
      reflection_scope = reflection.join_scopes(klass.arel_table, klass.predicate_builder, klass, record).inject(&:merge!)
    end

    [klass, reflection_scope]
  end.map do |(rhs_klass, reflection_scope), rs|
    preloader_for(reflection).new(rhs_klass, rs, reflection, scope, reflection_scope, associate_by_default)
  end
end