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, #loaded?, #method_missing, #proxy_respond_to?, #reload, #respond_to?, #target, #target=

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



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

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

#create(attributes = {}) ⇒ Object



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

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

#replace(obj, dont_save = false) ⇒ Object



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

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



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

def reset
  @target = nil
  @loaded = false
end