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

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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


8
9
10
11
12
13
14
15
16
# File 'activerecord/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 'activerecord/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 'activerecord/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 'activerecord/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 'activerecord/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 'activerecord/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 'activerecord/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


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

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)

43
44
45
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 43

def association_key_name
  raise NotImplementedError
end

#optionsObject


58
59
60
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 58

def options
  reflection.options
end

#owner_key_nameObject

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

Raises:

  • (NotImplementedError)

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

def owner_key_name
  raise NotImplementedError
end

#preload(preloader) ⇒ Object

Raises:

  • (NotImplementedError)

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

def preload(preloader)
  raise NotImplementedError
end

#query_scope(ids) ⇒ Object


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

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

#records_for(ids) ⇒ Object


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

def records_for(ids)
  query_scope(ids)
end

#run(preloader) ⇒ Object


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

def run(preloader)
  preload(preloader)
end

#scopeObject


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

def scope
  @scope ||= build_scope
end

#tableObject


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

def table
  klass.arel_table
end