Class: ActiveRecord::Associations::HasOneAssociation

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

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from AssociationProxy

#target

Instance Method Summary (collapse)

Methods inherited from SingularAssociation

#build, #create, #create!

Methods inherited from AssociationProxy

#===, #aliased_table_name, #initialize, #inspect, #loaded!, #loaded?, #method_missing, #proxy_owner, #proxy_reflection, #reload, #reset, #respond_to?, #scoped, #send, #stale_target?, #to_param

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

- (Object) replace(record, save = true)



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'activerecord/lib/active_record/associations/has_one_association.rb', line 5

def replace(record, save = true)
  record = check_record(record)
  load_target

  @reflection.klass.transaction do
    if @target && @target != record
      remove_target!(@reflection.options[:dependent])
    end

    if record
      set_inverse_instance(record)
      set_owner_attributes(record)

      if @owner.persisted? && save && !record.save
        nullify_owner_attributes(record)
        set_owner_attributes(@target)
        raise RecordNotSaved, "Failed to save the new associated #{@reflection.name}."
      end
    end
  end

  self.target = record
end