Class: ActiveRecord::Associations::SingularAssociation

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Association

#aliased_table_name, #association_scope, #create, #create!, #initialize, #initialize_attributes, #interpolate, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #remove_inverse_instance, #reset, #reset_scope, #scope, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

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

Instance Method Details

#build(attributes = {}) {|record| ... } ⇒ Object

Yields:

  • (record)


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

def build(attributes = {})
  record = build_record(attributes)
  yield(record) if block_given?
  set_new_record(record)
  record
end

#force_reload_readerObject

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



27
28
29
30
# File 'lib/active_record/associations/singular_association.rb', line 27

def force_reload_reader
  klass.uncached { reload }
  target
end

#readerObject

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



5
6
7
8
9
10
11
# File 'lib/active_record/associations/singular_association.rb', line 5

def reader
  if !loaded? || stale_target?
    reload
  end

  target
end

#writer(record) ⇒ Object

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



14
15
16
# File 'lib/active_record/associations/singular_association.rb', line 14

def writer(record)
  replace(record)
end