Class: ActiveRecord::Associations::BelongsToAssociation

Inherits:
AssociationProxy show all
Defined in:
lib/active_record/associations/belongs_to_association.rb

Overview

:nodoc:

Direct Known Subclasses

HasOneAssociation

Instance Method Summary collapse

Methods inherited from AssociationProxy

#initialize, #loaded?, #method_missing, #proxy_respond_to?, #reload, #respond_to?

Constructor Details

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveRecord::Associations::AssociationProxy

Instance Method Details

#build(attributes = {}) ⇒ Object



16
17
18
19
20
# File 'lib/active_record/associations/belongs_to_association.rb', line 16

def build(attributes = {})
  record = @association_class.new(attributes)
  replace(record, true)
  record
end

#create(attributes = {}) ⇒ Object



10
11
12
13
14
# File 'lib/active_record/associations/belongs_to_association.rb', line 10

def create(attributes = {})
  record = build(attributes)
  record.save
  record
end

#replace(obj, dont_save = false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_record/associations/belongs_to_association.rb', line 22

def replace(obj, dont_save = false)
  if obj.nil?
    @target = @owner[@association_class_primary_key_name] = nil
  else
    raise_on_type_mismatch(obj) unless obj.nil?

    @target = obj
    @owner[@association_class_primary_key_name] = obj.id unless obj.new_record?
  end
  @loaded = true

  return (@target.nil? ? nil : self)
end

#resetObject



5
6
7
8
# File 'lib/active_record/associations/belongs_to_association.rb', line 5

def reset
  @target = nil
  @loaded = false
end