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

#disable_joins, #owner, #reflection

Instance Method Summary collapse

Methods included from ForeignAssociation

#foreign_key_present?, #nullified_owner_attributes

Methods inherited from SingularAssociation

#build, #force_reload_reader, #reader, #reset, #writer

Methods inherited from Association

#async_load_target, #collection?, #create, #create!, #extensions, #initialize, #initialize_attributes, #inversed_from, #inversed_from_queries, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #remove_inverse_instance, #reset, #reset_negative_cache, #reset_scope, #scope, #set_inverse_instance, #set_inverse_instance_from_queries, #set_strict_loading, #stale_target?, #target, #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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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 :destroy_async
      if target.class.query_constraints_list
        primary_key_column = target.class.query_constraints_list
        id = primary_key_column.map { |col| target.public_send(col) }
      else
        primary_key_column = target.class.primary_key
        id = target.public_send(primary_key_column)
      end

      enqueue_destroy_association(
        owner_model_name: owner.class.to_s,
        owner_id: owner.id,
        association_class: reflection.klass.to_s,
        association_ids: [id],
        association_primary_key_column: primary_key_column,
        ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
      )
    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