Class: ActiveRecord::Associations::BelongsToAssociation

Inherits:
SingularAssociation show all
Defined in:
activerecord/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

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


37
38
39
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 37

def decrement_counters
  update_counters(-1)
end

#decrement_counters_before_last_saveObject


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 45

def decrement_counters_before_last_save
  if reflection.polymorphic?
    model_was = owner.attribute_before_last_save(reflection.foreign_type)&.constantize
  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


24
25
26
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 24

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
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 7

def handle_dependency
  return unless load_target

  case options[:dependent]
  when :destroy
    target.destroy
    raise ActiveRecord::Rollback unless target.destroyed?
  else
    target.send(options[:dependent])
  end
end

#increment_countersObject


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

def increment_counters
  update_counters(1)
end

#inversed_from(record) ⇒ Object


19
20
21
22
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 19

def inversed_from(record)
  replace_keys(record)
  super
end

#resetObject


28
29
30
31
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 28

def reset
  super
  @updated = false
end

#target_changed?Boolean


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

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

#updated?Boolean


33
34
35
# File 'activerecord/lib/active_record/associations/belongs_to_association.rb', line 33

def updated?
  @updated
end