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

#owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Association

#aliased_table_name, #association_scope, #initialize, #interpolate, #klass, #load_target, #loaded!, #loaded?, #reload, #reset, #reset_scope, #scoped, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

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

Instance Method Details

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

Yields:

  • (record)

28
29
30
31
32
33
# File 'lib/active_record/associations/singular_association.rb', line 28

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

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


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

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

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


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

def create!(attributes = {}, options = {}, &block)
  create_record(attributes, options, true, &block)
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
# File 'lib/active_record/associations/singular_association.rb', line 5

def reader(force_reload = false)
  if force_reload
    klass.uncached { reload }
  elsif !loaded? || stale_target?
    reload
  end

  target
end

#writer(record) ⇒ Object

Implements the writer method, e.g. foo.items= for Foo.has_many :items


16
17
18
# File 'lib/active_record/associations/singular_association.rb', line 16

def writer(record)
  replace(record)
end