Class: ActiveRecord::Associations::BelongsToAssociation
Overview
Instance Method Summary
collapse
#loaded, #loaded?, #proxy_respond_to?, #reload, #respond_to?, #target, #target=
Constructor Details
#initialize(owner, association_name, association_class_name, association_class_primary_key_name, options) ⇒ BelongsToAssociation
Returns a new instance of BelongsToAssociation.
5
6
7
8
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 5
def initialize(owner, association_name, association_class_name, association_class_primary_key_name, options)
super
construct_sql
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
in the class ActiveRecord::Associations::AssociationProxy
Instance Method Details
#build(attributes = {}) ⇒ Object
21
22
23
24
25
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 21
def build(attributes = {})
record = @association_class.new(attributes)
replace(record, true)
record
end
|
#create(attributes = {}) ⇒ Object
15
16
17
18
19
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 15
def create(attributes = {})
record = @association_class.create(attributes)
replace(record, true)
record
end
|
#replace(obj, dont_save = false) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 27
def replace(obj, dont_save = false)
if obj.nil?
@target = @owner[@association_class_primary_key_name] = nil
else
raise_on_type_mismatch(obj) unless obj.nil?
@target = (AssociationProxy === obj ? obj.target : obj)
@owner[@association_class_primary_key_name] = obj.id unless obj.new_record?
@updated = true
end
@loaded = true
return (@target.nil? ? nil : self)
end
|
#reset ⇒ Object
10
11
12
13
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 10
def reset
@target = nil
@loaded = false
end
|
#updated? ⇒ Boolean
42
43
44
|
# File 'lib/active_record/associations/belongs_to_association.rb', line 42
def updated?
@updated
end
|