Class: ActiveRecord::Associations::EmbedsOneAssociation

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

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

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



68
69
70
71
72
73
# File 'lib/active_record/associations/embeds_one_association.rb', line 68

def build(attributes = {}, options = {})
  build_record(attributes, options).tap do |record|
    yield(record) if block_given?
    replace(record, false)
  end
end

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



60
61
62
# File 'lib/active_record/associations/embeds_one_association.rb', line 60

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

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



64
65
66
# File 'lib/active_record/associations/embeds_one_association.rb', line 64

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

#reader(force_reload = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/associations/embeds_one_association.rb', line 36

def reader(force_reload = false)
  return @reader if @reader
  data = owner.read_attribute(reflection.name)
  if data.present?
    reflection.klass.from_hstore(data).tap do |inst|
      inst.send(:_owner=, owner)
      @reader = inst
    end
  end
end

#replace(record, save = true) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/active_record/associations/embeds_one_association.rb', line 51

def replace(record, save = true)
  @reader = record
  if record
    record.send(:_owner=, owner)
  end
  owner.send(:write_attribute, reflection.name, record ? record.to_hstore : nil)
  owner.save if save
end

#writer(record) ⇒ Object



47
48
49
# File 'lib/active_record/associations/embeds_one_association.rb', line 47

def writer(record)
  replace(record, false)
end