Class: ActiveRecord::Associations::Association

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

Overview

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, reflection) ⇒ Association

Returns a new instance of Association.



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

def initialize(owner, reflection)
  reflection.check_validity!

  @owner, @reflection = owner, reflection

  reset
  reset_scope
end

Instance Attribute Details

#inversedObject

Returns the value of attribute inversed.



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

def inversed
  @inversed
end

#ownerObject (readonly)

:nodoc:



20
21
22
# File 'lib/active_record/associations/association.rb', line 20

def owner
  @owner
end

#reflectionObject (readonly)

:nodoc:



20
21
22
# File 'lib/active_record/associations/association.rb', line 20

def reflection
  @reflection
end

#targetObject

:nodoc:



20
21
22
# File 'lib/active_record/associations/association.rb', line 20

def target
  @target
end

Instance Method Details

#aliased_table_nameObject

Returns the name of the table of the associated class:

post.comments.aliased_table_name # => "comments"


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

def aliased_table_name
  klass.table_name
end

#association_scopeObject

The scope for this association.

Note that the association_scope is merged into the target_scope only when the scope method is called. This is because at that point the call may be surrounded by scope.scoping { … } or with_scope { … } etc, which affects the scope which actually gets built.



101
102
103
104
105
# File 'lib/active_record/associations/association.rb', line 101

def association_scope
  if klass
    @association_scope ||= AssociationScope.new(self).scope
  end
end

#initialize_attributes(record) ⇒ Object

:nodoc:



175
176
177
178
179
180
# File 'lib/active_record/associations/association.rb', line 175

def initialize_attributes(record) #:nodoc:
  skip_assign = [reflection.foreign_key, reflection.type].compact
  attributes = create_scope.except(*(record.changed - skip_assign))
  record.assign_attributes(attributes)
  set_inverse_instance(record)
end

#interpolate(sql, record = nil) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/active_record/associations/association.rb', line 155

def interpolate(sql, record = nil)
  if sql.respond_to?(:to_proc)
    owner.instance_exec(record, &sql)
  else
    sql
  end
end

#klassObject

Returns the class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner.



122
123
124
# File 'lib/active_record/associations/association.rb', line 122

def klass
  reflection.klass
end

#load_targetObject

Loads the target if needed and returns it.

This method is abstract in the sense that it relies on find_target, which is expected to be provided by descendants.

If the target is already loaded it is just returned. Thus, you can call load_target unconditionally to get the target.

ActiveRecord::RecordNotFound is rescued within the method, and it is not reraised. The proxy is reset and nil is the return value.



146
147
148
149
150
151
152
153
# File 'lib/active_record/associations/association.rb', line 146

def load_target
  @target = find_target if (@stale_state && stale_target?) || find_target?

  loaded! unless loaded?
  target
rescue ActiveRecord::RecordNotFound
  reset
end

#loaded!Object

Asserts the target has been loaded setting the loaded flag to true.



64
65
66
67
68
# File 'lib/active_record/associations/association.rb', line 64

def loaded!
  @loaded = true
  @stale_state = stale_state
  @inversed = false
end

#loaded?Boolean

Has the target been already loaded?

Returns:

  • (Boolean)


59
60
61
# File 'lib/active_record/associations/association.rb', line 59

def loaded?
  @loaded
end

#marshal_dumpObject

We can’t dump @reflection since it contains the scope proc



164
165
166
167
# File 'lib/active_record/associations/association.rb', line 164

def marshal_dump
  ivars = (instance_variables - [:@reflection]).map { |name| [name, instance_variable_get(name)] }
  [@reflection.name, ivars]
end

#marshal_load(data) ⇒ Object



169
170
171
172
173
# File 'lib/active_record/associations/association.rb', line 169

def marshal_load(data)
  reflection_name, ivars = data
  ivars.each { |name, val| instance_variable_set(name, val) }
  @reflection = @owner.class.reflect_on_association(reflection_name)
end

#reloadObject

Reloads the target and returns self on success.



51
52
53
54
55
56
# File 'lib/active_record/associations/association.rb', line 51

def reload
  reset
  reset_scope
  load_target
  self unless target.nil?
end

#resetObject

Resets the loaded flag to false and sets the target to nil.



43
44
45
46
47
48
# File 'lib/active_record/associations/association.rb', line 43

def reset
  @loaded = false
  @target = nil
  @stale_state = nil
  @inversed = false
end

#reset_scopeObject



107
108
109
# File 'lib/active_record/associations/association.rb', line 107

def reset_scope
  @association_scope = nil
end

#scopeObject



86
87
88
# File 'lib/active_record/associations/association.rb', line 86

def scope
  target_scope.merge(association_scope)
end

#scopedObject



90
91
92
93
# File 'lib/active_record/associations/association.rb', line 90

def scoped
  ActiveSupport::Deprecation.warn "#scoped is deprecated. use #scope instead."
  scope
end

#set_inverse_instance(record) ⇒ Object

Set the inverse association, if possible



112
113
114
115
116
117
118
# File 'lib/active_record/associations/association.rb', line 112

def set_inverse_instance(record)
  if record && invertible_for?(record)
    inverse = record.association(inverse_reflection_for(record).name)
    inverse.target = owner
    inverse.inversed = true
  end
end

#stale_target?Boolean

The target is stale if the target no longer points to the record(s) that the relevant foreign_key(s) refers to. If stale, the association accessor method on the owner will reload the target. It’s up to subclasses to implement the stale_state method if relevant.

Note that if the target has not been loaded, it is not considered stale.

Returns:

  • (Boolean)


76
77
78
# File 'lib/active_record/associations/association.rb', line 76

def stale_target?
  !inversed && loaded? && @stale_state != stale_state
end

#target_scopeObject

Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope)



128
129
130
131
132
133
134
# File 'lib/active_record/associations/association.rb', line 128

def target_scope
  all = klass.all
  scope = AssociationRelation.new(klass, klass.arel_table, self)
  scope.merge! all
  scope.default_scoped = all.default_scoped?
  scope
end