Class: ActiveRecord::Associations::BelongsToAssociation
Overview
Active Record Belongs To Association
Instance Attribute Summary
Attributes inherited from Association
#disable_joins, #owner, #reflection, #target
Instance Method Summary
collapse
#build, #force_reload_reader, #reader, #writer
Methods inherited from Association
#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, #stale_target?
Instance Method Details
#decrement_counters ⇒ Object
53
54
55
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 53
def decrement_counters
update_counters(-1)
end
|
#decrement_counters_before_last_save ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 61
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
40
41
42
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 40
def default(&block)
writer(owner.instance_exec(&block)) if reader.nil?
end
|
#handle_dependency ⇒ Object
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
|
# 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
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_counters ⇒ Object
57
58
59
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 57
def increment_counters
update_counters(1)
end
|
#inversed_from(record) ⇒ Object
35
36
37
38
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 35
def inversed_from(record)
replace_keys(record)
super
end
|
#reset ⇒ Object
44
45
46
47
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 44
def reset
super
@updated = false
end
|
#saved_change_to_target? ⇒ Boolean
84
85
86
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 84
def saved_change_to_target?
owner.saved_change_to_attribute?(reflection.foreign_key)
end
|
#target_changed? ⇒ Boolean
76
77
78
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 76
def target_changed?
owner.attribute_changed?(reflection.foreign_key) || (!foreign_key_present? && target&.new_record?)
end
|
#target_previously_changed? ⇒ Boolean
80
81
82
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 80
def target_previously_changed?
owner.attribute_previously_changed?(reflection.foreign_key)
end
|
#updated? ⇒ Boolean
49
50
51
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 49
def updated?
@updated
end
|