Class: ActiveRecord::Associations::Preloader::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/associations/preloader/association.rb

Overview

:nodoc:

Direct Known Subclasses

ThroughAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, owners, reflection, preload_scope) ⇒ Association

Returns a new instance of Association.



9
10
11
12
13
14
15
16
# File 'lib/active_record/associations/preloader/association.rb', line 9

def initialize(klass, owners, reflection, preload_scope)
  @klass         = klass
  @owners        = owners
  @reflection    = reflection
  @preload_scope = preload_scope
  @model         = owners.first && owners.first.class
  @preloaded_records = []
end

Instance Attribute Details

#preloaded_recordsObject (readonly)

Returns the value of attribute preloaded_records.



7
8
9
# File 'lib/active_record/associations/preloader/association.rb', line 7

def preloaded_records
  @preloaded_records
end

Instance Method Details

#run(preloader) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/active_record/associations/preloader/association.rb', line 18

def run(preloader)
  records = load_records do |record|
    owner = owners_by_key[convert_key(record[association_key_name])]
    association = owner.association(reflection.name)
    association.set_inverse_instance(record)
  end

  owners.each do |owner|
    associate_records_to_owner(owner, records[convert_key(owner[owner_key_name])] || [])
  end
end