Class: ActiveRecord::Associations::HasOneAssociation

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

Overview

Active Record Has One Association

Direct Known Subclasses

HasOneThroughAssociation

Instance Attribute Summary

Attributes inherited from Association

#inversed, #owner, #reflection, #target

Instance Method Summary collapse

Methods included from ForeignAssociation

#foreign_key_present?

Methods inherited from SingularAssociation

#build, #force_reload_reader, #reader, #writer

Methods inherited from Association

#association_scope, #create, #create!, #extensions, #initialize, #initialize_attributes, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #remove_inverse_instance, #reset, #reset_scope, #scope, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

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

Instance Method Details

#delete(method = options[:dependent]) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/associations/has_one_association.rb', line 55

def delete(method = options[:dependent])
  if load_target
    case method
    when :delete
      target.delete
    when :destroy
      target.destroyed_by_association = reflection
      target.destroy
      throw(:abort) unless target.destroyed?
    when :nullify
      target.update_columns(reflection.foreign_key => nil) if target.persisted?
    end
  end
end

#handle_dependencyObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_record/associations/has_one_association.rb', line 9

def handle_dependency
  case options[:dependent]
  when :restrict_with_exception
    raise ActiveRecord::DeleteRestrictionError.new(reflection.name) if load_target

  when :restrict_with_error
    if load_target
      record = owner.class.human_attribute_name(reflection.name).downcase
      owner.errors.add(:base, :'restrict_dependent_destroy.has_one', record: record)
      throw(:abort)
    end

  else
    delete
  end
end

#replace(record, save = true) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record/associations/has_one_association.rb', line 26

def replace(record, save = true)
  raise_on_type_mismatch!(record) if record
  load_target

  return target unless target || record

  assigning_another_record = target != record
  if assigning_another_record || record.has_changes_to_save?
    save &&= owner.persisted?

    transaction_if(save) do
      remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record

      if record
        set_owner_attributes(record)
        set_inverse_instance(record)

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

  self.target = record
end