Class: ActiveRecord::Associations::SingularAssociation

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

Overview

:nodoc:

Direct Known Subclasses

BelongsToAssociation, HasOneAssociation

Instance Attribute Summary

Attributes inherited from Association

#disable_joins, #owner, #reflection

Instance Method Summary collapse

Methods inherited from Association

#async_load_target, #collection?, #create, #create!, #extensions, #initialize, #initialize_attributes, #inversed_from, #inversed_from_queries, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #remove_inverse_instance, #reset_negative_cache, #reset_scope, #scope, #set_inverse_instance, #set_inverse_instance_from_queries, #set_strict_loading, #stale_target?, #target, #target=

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::Association

Instance Method Details

#build(attributes = nil, &block) ⇒ Object



29
30
31
32
33
# File '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

#force_reload_readerObject

Implements the reload reader method, e.g. foo.reload_bar for Foo.has_one :bar



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

def force_reload_reader
  reload(true)
  target
end

#readerObject

Implements the reader method, e.g. foo.bar for Foo.has_one :bar



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

def reader
  ensure_klass_exists!

  if !loaded? || stale_target?
    reload
  end

  target
end

#resetObject

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



18
19
20
21
22
# File 'lib/active_record/associations/singular_association.rb', line 18

def reset
  super
  @target = nil
  @future_target = nil
end

#writer(record) ⇒ Object

Implements the writer method, e.g. foo.bar= for Foo.belongs_to :bar



25
26
27
# File 'lib/active_record/associations/singular_association.rb', line 25

def writer(record)
  replace(record)
end