Class: ActiveRecord::Associations::BelongsToAssociation

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

Overview

Active Record Belongs To Association

Direct Known Subclasses

BelongsToPolymorphicAssociation

Instance Attribute Summary

Attributes inherited from Association

#disable_joins, #owner, #reflection

Instance Method Summary collapse

Methods inherited from SingularAssociation

#build, #force_reload_reader, #reader, #writer

Methods inherited from Association

#async_load_target, #collection?, #create, #create!, #extensions, #initialize, #initialize_attributes, #inversed_from_queries, #klass, #load_target, #loaded!, #loaded?, #marshal_dump, #marshal_load, #reload, #remove_inverse_instance, #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

#decrement_countersObject



59
60
61
# File 'lib/active_record/associations/belongs_to_association.rb', line 59

def decrement_counters
  update_counters(-1)
end

#decrement_counters_before_last_saveObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/active_record/associations/belongs_to_association.rb', line 67

def decrement_counters_before_last_save
  if reflection.polymorphic?
    model_type_was = owner.attribute_before_last_save(reflection.foreign_type)
    model_was = owner.class.polymorphic_class_for(model_type_was) if model_type_was
  else
    model_was = klass
  end

  foreign_key_was = owner.attribute_before_last_save(reflection.foreign_key)

  if foreign_key_was && model_was < ActiveRecord::Base
    update_counters_via_scope(model_was, foreign_key_was, -1)
  end
end

#default(&block) ⇒ Object



46
47
48
# File 'lib/active_record/associations/belongs_to_association.rb', line 46

def default(&block)
  writer(owner.instance_exec(&block)) if reader.nil?
end

#handle_dependencyObject

:nodoc:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/active_record/associations/belongs_to_association.rb', line 7

def handle_dependency
  return unless load_target

  case options[:dependent]
  when :destroy
    raise ActiveRecord::Rollback unless target.destroy
  when :destroy_async
    if reflection.foreign_key.is_a?(Array)
      primary_key_column = reflection.active_record_primary_key
      id = reflection.foreign_key.map { |col| owner.public_send(col) }
    else
      primary_key_column = reflection.active_record_primary_key
      id = owner.public_send(reflection.foreign_key)
    end

    association_class = if reflection.polymorphic?
      owner.public_send(reflection.foreign_type)
    else
      reflection.klass
    end

    enqueue_destroy_association(
      owner_model_name: owner.class.to_s,
      owner_id: owner.id,
      association_class: association_class.to_s,
      association_ids: [id],
      association_primary_key_column: primary_key_column,
      ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
    )
  else
    target.public_send(options[:dependent])
  end
end

#increment_countersObject



63
64
65
# File 'lib/active_record/associations/belongs_to_association.rb', line 63

def increment_counters
  update_counters(1)
end

#inversed_from(record) ⇒ Object



41
42
43
44
# File 'lib/active_record/associations/belongs_to_association.rb', line 41

def inversed_from(record)
  replace_keys(record)
  super
end

#resetObject



50
51
52
53
# File 'lib/active_record/associations/belongs_to_association.rb', line 50

def reset
  super
  @updated = false
end

#saved_change_to_target?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_record/associations/belongs_to_association.rb', line 90

def saved_change_to_target?
  owner.saved_change_to_attribute?(reflection.foreign_key)
end

#target_changed?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/active_record/associations/belongs_to_association.rb', line 82

def target_changed?
  owner.attribute_changed?(reflection.foreign_key) || (!foreign_key_present? && target&.new_record?)
end

#target_previously_changed?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/active_record/associations/belongs_to_association.rb', line 86

def target_previously_changed?
  owner.attribute_previously_changed?(reflection.foreign_key)
end

#updated?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_record/associations/belongs_to_association.rb', line 55

def updated?
  @updated
end