Class: ActiveRecord::Associations::SingularAssociation
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
|
|
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Association
|
|
|
Instance Chain:
self,
Association
|
|
| Inherits: |
ActiveRecord::Associations::Association
|
| Defined in: | activerecord/lib/active_record/associations/singular_association.rb |
Class Method Summary
Association - Inherited
Instance Attribute Summary
Association - Inherited
| #collection? | Whether the association represent a single record or a collection of records. |
| #disable_joins, | |
| #loaded? | Has the target been already loaded? |
| #options, #owner, #reflection, | |
| #stale_target? | The target is stale if the target no longer points to the record(s) that the relevant foreign_key(s) refers to. |
| #target, | |
| #target= | Sets the target of this association to |
| #find_target?, | |
| #foreign_key_present? | Returns true if there is a foreign key present on the owner which references the target. |
| #violates_strict_loading? | |
Instance Method Summary
- #build(attributes = nil, &block)
-
#force_reload_reader
Implements the reload reader method, e.g.
-
#reader
Implements the reader method, e.g.
-
#reset
Resets the loaded flag to
falseand sets the target tonil. -
#writer(record)
Implements the writer method, e.g.
- #_create_record(attributes, raise_error = false, &block) private
- #find_target(async: false) private
- #replace(record) private
- #scope_for_create private
- #set_new_record(record) private
Association - Inherited
| #async_load_target, #create, #create!, #extensions, #initialize_attributes, #inversed_from, #inversed_from_queries, | |
| #klass | Returns the class of the target. |
| #load_target | Loads the target if needed and returns it. |
| #loaded! | Asserts the target has been loaded setting the loaded flag to |
| #marshal_dump | We can’t dump @reflection and @through_reflection since it contains the scope proc. |
| #marshal_load, | |
| #reload | Reloads the target and returns |
| #remove_inverse_instance | Remove the inverse association, if possible. |
| #reset | Resets the loaded flag to |
| #reset_negative_cache, #reset_scope, #scope, | |
| #set_inverse_instance | Set the inverse association, if possible. |
| #set_inverse_instance_from_queries, #set_strict_loading, | |
| #association_scope | The scope for this association. |
| #build_record, #enqueue_destroy_association, | |
| #ensure_klass_exists! | Reader and writer methods call this so that consistent errors are presented when the association target class does not exist. |
| #find_target, | |
| #foreign_key_for? | Returns true if record contains the foreign_key. |
| #inversable?, #inverse_association_for, | |
| #inverse_reflection_for | Can be redefined by subclasses, notably polymorphic belongs_to The record parameter is necessary to support polymorphic inverses as we must check for the association in the specific class of the record. |
| #invertible_for? | Returns true if inverse association on the given record needs to be set. |
| #matches_foreign_key?, | |
| #raise_on_type_mismatch! | Raises |
| #scope_for_create, | |
| #skip_statement_cache? | Returns true if statement cache should be skipped on the association reader. |
| #skip_strict_loading, | |
| #stale_state | This should be implemented to return the values of the relevant key(s) on the owner, so that when stale_state is different from the value stored on the last find_target, the target is stale. |
| #target_scope | Can be overridden (i.e. in |
Constructor Details
This class inherits a constructor from ActiveRecord::Associations::Association
Instance Method Details
#_create_record(attributes, raise_error = false, &block) (private)
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 67
def _create_record(attributes, raise_error = false, &block) record = build_record(attributes, &block) saved = record.save set_new_record(record) raise RecordInvalid.new(record) if !saved && raise_error record end
#build(attributes = nil, &block)
[ GitHub ]# File 'activerecord/lib/active_record/associations/singular_association.rb', line 29
def build(attributes = nil, &block) record = build_record(attributes, &block) set_new_record(record) record end
#find_target(async: false) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/singular_association.rb', line 47
def find_target(async: false) if disable_joins if async scope.load_async.then(&:first) else scope.first end else super.then(&:first) end end
#force_reload_reader
Implements the reload reader method, e.g. foo.reload_bar for Foo.has_one :bar
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 37
def force_reload_reader reload(true) target end
#reader
Implements the reader method, e.g. foo.bar for Foo.has_one :bar
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 7
def reader ensure_klass_exists! if !loaded? || stale_target? reload end target end
#replace(record) (private)
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 59
def replace(record) raise NotImplementedError, "Subclasses must implement a replace(record) method" end
#reset
Resets the loaded flag to false and sets the target to nil.
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 18
def reset super @target = nil @future_target = nil end
#scope_for_create (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/singular_association.rb', line 43
def scope_for_create super.except!(*Array(klass.primary_key)) end
#set_new_record(record) (private)
[ GitHub ]# File 'activerecord/lib/active_record/associations/singular_association.rb', line 63
def set_new_record(record) replace(record) end
#writer(record)
Implements the writer method, e.g. foo.bar= for Foo.belongs_to :bar
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 25
def writer(record) replace(record) end