Class: ActiveRecord::Associations::BelongsToAssociation

Inherits:
SingularAssociation show all
Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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, #target

Instance Method Summary collapse

Methods inherited from SingularAssociation

#build, #force_reload_reader, #reader, #writer

Methods inherited from Association

#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, #stale_target?

Constructor Details

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

Instance Method Details

#decrement_countersObject



48
49
50
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 48

def decrement_counters
  update_counters(-1)
end

#decrement_counters_before_last_saveObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 56

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



35
36
37
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 35

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
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/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
    id = owner.public_send(reflection.foreign_key.to_sym)
    primary_key_column = reflection.active_record_primary_key.to_sym

    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)
    )
  else
    target.public_send(options[:dependent])
  end
end

#increment_countersObject



52
53
54
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 52

def increment_counters
  update_counters(1)
end

#inversed_from(record) ⇒ Object



30
31
32
33
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 30

def inversed_from(record)
  replace_keys(record)
  super
end

#resetObject



39
40
41
42
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 39

def reset
  super
  @updated = false
end

#saved_change_to_target?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 79

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

#target_changed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 71

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

#target_previously_changed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 75

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

#updated?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/associations/belongs_to_association.rb', line 44

def updated?
  @updated
end