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

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

Overview

:nodoc:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Association) initialize(klass, owners, reflection, preload_options)

A new instance of Association



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

def initialize(klass, owners, reflection, preload_options)
  @klass           = klass
  @owners          = owners
  @reflection      = reflection
  @preload_options = preload_options || {}
  @model           = owners.first && owners.first.class
  @scoped          = nil
  @owners_by_key   = nil
end

Instance Attribute Details

- (Object) klass (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

- (Object) model (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

- (Object) owners (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

- (Object) preload_options (readonly)

Returns the value of attribute preload_options



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

def preload_options
  @preload_options
end

- (Object) reflection (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

- (Object) association_key

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



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

def association_key
  table[association_key_name]
end

- (Object) association_key_name

The name of the key on the associated records

Raises:

  • (NotImplementedError)


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

def association_key_name
  raise NotImplementedError
end

- (Object) options



64
65
66
# File 'activerecord/lib/active_record/associations/preloader/association.rb', line 64

def options
  reflection.options
end

- (Object) owner_key_name

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

Raises:

  • (NotImplementedError)


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

def owner_key_name
  raise NotImplementedError
end

- (Object) owners_by_key

We're converting to a string here because postgres will return the aliased association key in a habtm as a string (for whatever reason)



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

def owners_by_key
  @owners_by_key ||= owners.group_by do |owner|
    key = owner[owner_key_name]
    key && key.to_s
  end
end

- (Object) preload

Raises:

  • (NotImplementedError)


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

def preload
  raise NotImplementedError
end

- (Object) records_for(ids)



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

def records_for(ids)
  scoped.where(association_key.in(ids))
end

- (Object) run



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

def run
  unless owners.first.association(reflection.name).loaded?
    preload
  end
end

- (Object) scoped



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

def scoped
  @scoped ||= build_scope
end

- (Object) table



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

def table
  klass.arel_table
end