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

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

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Association.



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

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

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/active_record/associations/preloader/association.rb', line 5

def klass
  @klass
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/active_record/associations/preloader/association.rb', line 5

def model
  @model
end

#ownersObject (readonly)

Returns the value of attribute owners.



5
6
7
# File 'lib/active_record/associations/preloader/association.rb', line 5

def owners
  @owners
end

#preload_scopeObject (readonly)

Returns the value of attribute preload_scope.



5
6
7
# File 'lib/active_record/associations/preloader/association.rb', line 5

def preload_scope
  @preload_scope
end

#preloaded_recordsObject (readonly)

Returns the value of attribute preloaded_records.



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

def preloaded_records
  @preloaded_records
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



5
6
7
# File 'lib/active_record/associations/preloader/association.rb', line 5

def reflection
  @reflection
end

Instance Method Details

#association_keyObject

This is overridden by HABTM as the condition should be on the foreign_key column in the join table



45
46
47
# File 'lib/active_record/associations/preloader/association.rb', line 45

def association_key
  klass.arel_attribute(association_key_name, table)
end

#association_key_nameObject

The name of the key on the associated records

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/active_record/associations/preloader/association.rb', line 39

def association_key_name
  raise NotImplementedError
end

#optionsObject



54
55
56
# File 'lib/active_record/associations/preloader/association.rb', line 54

def options
  reflection.options
end

#owner_key_nameObject

The name of the key on the model which declares the association

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/active_record/associations/preloader/association.rb', line 50

def owner_key_name
  raise NotImplementedError
end

#preload(preloader) ⇒ Object

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/active_record/associations/preloader/association.rb', line 22

def preload(preloader)
  raise NotImplementedError
end

#records_for(ids) ⇒ Object



30
31
32
# File 'lib/active_record/associations/preloader/association.rb', line 30

def records_for(ids)
  scope.where(association_key_name => ids)
end

#run(preloader) ⇒ Object



18
19
20
# File 'lib/active_record/associations/preloader/association.rb', line 18

def run(preloader)
  preload(preloader)
end

#scopeObject



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

def scope
  @scope ||= build_scope
end

#tableObject



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

def table
  klass.arel_table
end