Class: ActiveRecord::Associations::Association
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::Association
- Defined in:
- lib/active_record/associations/association.rb
Overview
Active Record Associations
This is the root class of all associations (‘+ Foo’ signifies an included module Foo):
Association
SingularAssociation
HasOneAssociation + ForeignAssociation
HasOneThroughAssociation + ThroughAssociation
BelongsToAssociation
BelongsToPolymorphicAssociation
CollectionAssociation
HasManyAssociation + ForeignAssociation
HasManyThroughAssociation + ThroughAssociation
Direct Known Subclasses
Instance Attribute Summary collapse
-
#inversed ⇒ Object
Returns the value of attribute inversed.
-
#owner ⇒ Object
readonly
:nodoc:.
-
#reflection ⇒ Object
readonly
:nodoc:.
-
#target ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#association_scope ⇒ Object
The scope for this association.
- #create(attributes = {}, &block) ⇒ Object
- #create!(attributes = {}, &block) ⇒ Object
- #extensions ⇒ Object
-
#initialize(owner, reflection) ⇒ Association
constructor
A new instance of Association.
-
#initialize_attributes(record, except_from_scope_attributes = nil) ⇒ Object
:nodoc:.
-
#klass ⇒ Object
Returns the class of the target.
-
#load_target ⇒ Object
Loads the target if needed and returns it.
-
#loaded! ⇒ Object
Asserts the target has been loaded setting the loaded flag to
true
. -
#loaded? ⇒ Boolean
Has the target been already loaded?.
-
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc.
- #marshal_load(data) ⇒ Object
-
#reload ⇒ Object
Reloads the target and returns
self
on success. -
#remove_inverse_instance(record) ⇒ Object
Remove the inverse association, if possible.
-
#reset ⇒ Object
Resets the loaded flag to
false
and sets the target tonil
. - #reset_scope ⇒ Object
- #scope ⇒ Object
-
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible.
-
#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.
-
#target_scope ⇒ Object
Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope).
Constructor Details
#initialize(owner, reflection) ⇒ Association
Returns a new instance of Association.
26 27 28 29 30 31 32 33 |
# File 'lib/active_record/associations/association.rb', line 26 def initialize(owner, reflection) reflection.check_validity! @owner, @reflection = owner, reflection reset reset_scope end |
Instance Attribute Details
#inversed ⇒ Object
Returns the value of attribute inversed.
22 23 24 |
# File 'lib/active_record/associations/association.rb', line 22 def inversed @inversed end |
#owner ⇒ Object (readonly)
:nodoc:
21 22 23 |
# File 'lib/active_record/associations/association.rb', line 21 def owner @owner end |
#reflection ⇒ Object (readonly)
:nodoc:
21 22 23 |
# File 'lib/active_record/associations/association.rb', line 21 def reflection @reflection end |
#target ⇒ Object
:nodoc:
21 22 23 |
# File 'lib/active_record/associations/association.rb', line 21 def target @target end |
Instance Method Details
#association_scope ⇒ Object
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.
89 90 91 92 93 |
# File 'lib/active_record/associations/association.rb', line 89 def association_scope if klass @association_scope ||= AssociationScope.scope(self) end end |
#create(attributes = {}, &block) ⇒ Object
181 182 183 |
# File 'lib/active_record/associations/association.rb', line 181 def create(attributes = {}, &block) _create_record(attributes, &block) end |
#create!(attributes = {}, &block) ⇒ Object
185 186 187 |
# File 'lib/active_record/associations/association.rb', line 185 def create!(attributes = {}, &block) _create_record(attributes, true, &block) end |
#extensions ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/active_record/associations/association.rb', line 130 def extensions extensions = klass.default_extensions | reflection.extensions if reflection.scope extensions |= reflection.scope_for(klass.unscoped, owner).extensions end extensions end |
#initialize_attributes(record, except_from_scope_attributes = nil) ⇒ Object
:nodoc:
171 172 173 174 175 176 177 178 179 |
# File 'lib/active_record/associations/association.rb', line 171 def initialize_attributes(record, except_from_scope_attributes = nil) #:nodoc: except_from_scope_attributes ||= {} skip_assign = [reflection.foreign_key, reflection.type].compact assigned_keys = record.changed_attribute_names_to_save assigned_keys += except_from_scope_attributes.keys.map(&:to_s) attributes = scope_for_create.except!(*(assigned_keys - skip_assign)) record.send(:_assign_attributes, attributes) if attributes.any? set_inverse_instance(record) end |
#klass ⇒ Object
Returns the class of the target. belongs_to polymorphic overrides this to look at the polymorphic_type field on the owner.
120 121 122 |
# File 'lib/active_record/associations/association.rb', line 120 def klass reflection.klass end |
#load_target ⇒ Object
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.
150 151 152 153 154 155 156 157 |
# File 'lib/active_record/associations/association.rb', line 150 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
.
57 58 59 60 61 |
# File 'lib/active_record/associations/association.rb', line 57 def loaded! @loaded = true @stale_state = stale_state @inversed = false end |
#loaded? ⇒ Boolean
Has the target been already loaded?
52 53 54 |
# File 'lib/active_record/associations/association.rb', line 52 def loaded? @loaded end |
#marshal_dump ⇒ Object
We can’t dump @reflection and @through_reflection since it contains the scope proc
160 161 162 163 |
# File 'lib/active_record/associations/association.rb', line 160 def marshal_dump ivars = (instance_variables - [:@reflection, :@through_reflection]).map { |name| [name, instance_variable_get(name)] } [@reflection.name, ivars] end |
#marshal_load(data) ⇒ Object
165 166 167 168 169 |
# File 'lib/active_record/associations/association.rb', line 165 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 |
#reload ⇒ Object
Reloads the target and returns self
on success.
44 45 46 47 48 49 |
# File 'lib/active_record/associations/association.rb', line 44 def reload reset reset_scope load_target self unless target.nil? end |
#remove_inverse_instance(record) ⇒ Object
Remove the inverse association, if possible
110 111 112 113 114 115 116 |
# File 'lib/active_record/associations/association.rb', line 110 def remove_inverse_instance(record) if invertible_for?(record) inverse = record.association(inverse_reflection_for(record).name) inverse.target = nil inverse.inversed = false end end |
#reset ⇒ Object
Resets the loaded flag to false
and sets the target to nil
.
36 37 38 39 40 41 |
# File 'lib/active_record/associations/association.rb', line 36 def reset @loaded = false @target = nil @stale_state = nil @inversed = false end |
#reset_scope ⇒ Object
95 96 97 |
# File 'lib/active_record/associations/association.rb', line 95 def reset_scope @association_scope = nil end |
#scope ⇒ Object
79 80 81 |
# File 'lib/active_record/associations/association.rb', line 79 def scope target_scope.merge!(association_scope) end |
#set_inverse_instance(record) ⇒ Object
Set the inverse association, if possible
100 101 102 103 104 105 106 107 |
# File 'lib/active_record/associations/association.rb', line 100 def set_inverse_instance(record) if invertible_for?(record) inverse = record.association(inverse_reflection_for(record).name) inverse.target = owner inverse.inversed = true end record 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.
69 70 71 |
# File 'lib/active_record/associations/association.rb', line 69 def stale_target? !inversed && loaded? && @stale_state != stale_state end |
#target_scope ⇒ Object
Can be overridden (i.e. in ThroughAssociation) to merge in other scopes (i.e. the through association’s scope)
126 127 128 |
# File 'lib/active_record/associations/association.rb', line 126 def target_scope AssociationRelation.create(klass, self).merge!(klass.all) end |