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 Method Summary collapse

Constructor Details

#initialize(klass, owners, reflection, preload_scope, associate_by_default = true) ⇒ Association

Returns a new instance of Association.



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

def initialize(klass, owners, reflection, preload_scope, associate_by_default = true)
  @klass         = klass
  @owners        = owners.uniq(&:__id__)
  @reflection    = reflection
  @preload_scope = preload_scope
  @associate     = associate_by_default || !preload_scope || preload_scope.empty_scope?
  @model         = owners.first && owners.first.class
end

Instance Method Details

#preloaded_recordsObject



32
33
34
35
36
# File 'lib/active_record/associations/preloader/association.rb', line 32

def preloaded_records
  load_records unless defined?(@preloaded_records)

  @preloaded_records
end

#records_by_ownerObject



26
27
28
29
30
# File 'lib/active_record/associations/preloader/association.rb', line 26

def records_by_owner
  load_records unless defined?(@records_by_owner)

  @records_by_owner
end

#runObject



16
17
18
19
20
21
22
23
24
# File 'lib/active_record/associations/preloader/association.rb', line 16

def run
  records = records_by_owner

  owners.each do |owner|
    associate_records_to_owner(owner, records[owner] || [])
  end if @associate

  self
end