Class: ActiveRecord::Associations::BelongsToAssociation
Overview
Active Record Belongs To Association
Instance Attribute Summary
Attributes inherited from Association
#owner, #reflection, #target
Instance Method Summary
collapse
#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_scope, #scope, #set_inverse_instance, #set_inverse_instance_from_queries, #stale_target?
Instance Method Details
#decrement_counters ⇒ Object
37
38
39
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 37
def decrement_counters
update_counters(-1)
end
|
#decrement_counters_before_last_save ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File '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).try(: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 'lib/active_record/associations/belongs_to_association.rb', line 24
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
|
# File '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_counters ⇒ Object
41
42
43
|
# File '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 'lib/active_record/associations/belongs_to_association.rb', line 19
def inversed_from(record)
replace_keys(record)
super
end
|
#reset ⇒ Object
28
29
30
31
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 28
def reset
super
@updated = false
end
|
#target_changed? ⇒ Boolean
59
60
61
|
# File '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 'lib/active_record/associations/belongs_to_association.rb', line 33
def updated?
@updated
end
|