Class: ActiveRecord::Associations::HasOneAssociation

Inherits:
BelongsToAssociation show all
Defined in:
lib/active_record/associations/has_one_association.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods inherited from BelongsToAssociation

#build, #create, #nil?, #reload, #reset

Methods inherited from AssociationProxy

#loaded?, #method_missing, #proxy_respond_to?, #respond_to?

Constructor Details

#initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) ⇒ HasOneAssociation

Returns a new instance of HasOneAssociation.



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

def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
  super

  construct_sql
end

Dynamic Method Handling

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

Instance Method Details

#replace(obj, dont_save = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/associations/has_one_association.rb', line 10

def replace(obj, dont_save = false)
  load_target
  unless @target.nil?
    @target[@association_class_primary_key_name] = nil
    @target.save unless @owner.new_record?
  end

  if obj.nil?
    @target = nil
  else
    raise_on_type_mismatch(obj)
    
    obj[@association_class_primary_key_name] = @owner.id unless @owner.new_record?
    @target = obj
  end

  @loaded = true
  unless @owner.new_record? or obj.nil? or dont_save
    return (obj.save ? obj : false)
  else
    return obj
  end
end