Class: ActiveRecord::Associations::SingularAssociation

Inherits:
Association show all
Defined in:
activerecord/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, #initialize, #initialize_attributes, #interpolate, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #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)

33
34
35
36
37
38
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 33

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

#create(attributes = {}, &block) ⇒ Object


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

def create(attributes = {}, &block)
  _create_record(attributes, &block)
end

#create!(attributes = {}, &block) ⇒ Object


29
30
31
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 29

def create!(attributes = {}, &block)
  _create_record(attributes, true, &block)
end

#force_reload_readerObject

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


42
43
44
45
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 42

def force_reload_reader
  klass.uncached { reload }
  target
end

#reader(force_reload = false) ⇒ Object

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


5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'activerecord/lib/active_record/associations/singular_association.rb', line 5

def reader(force_reload = false)
  if force_reload && klass
    ActiveSupport::Deprecation.warn(<<-MSG.squish)
      Passing an argument to force an association to reload is now
      deprecated and will be removed in Rails 5.1. Please call `reload_#{reflection.name}` instead.
    MSG

    klass.uncached { reload }
  elsif !loaded? || stale_target?
    reload
  end

  target
end

#writer(record) ⇒ Object

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


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

def writer(record)
  replace(record)
end