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

#owner, #reflection, #target

Instance Method Summary collapse

Methods included from ForeignAssociation

#foreign_key_present?, #nullified_owner_attributes

Methods inherited from SingularAssociation

#build, #force_reload_reader, #reader, #writer

Methods inherited from Association

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

Constructor Details

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

Instance Method Details

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/associations/has_one_association.rb', line 26

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(nullified_owner_attributes) 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