Class: PassiveRecord::Associations::HasOneRelation

Inherits:
Relation
  • Object
show all
Defined in:
lib/passive_record/associations/has_one.rb

Direct Known Subclasses

HasManyRelation

Instance Attribute Summary

Attributes inherited from Relation

#association, #parent_model

Instance Method Summary collapse

Methods inherited from Relation

#singular?

Instance Method Details

#child_classObject



40
41
42
# File 'lib/passive_record/associations/has_one.rb', line 40

def child_class
  Object.const_get(association.child_class_name.singularize)
end

#create(attrs = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/passive_record/associations/has_one.rb', line 24

def create(attrs={})
  child_class.create(
    attrs.merge(
      parent_model_id_field => parent_model.id
    )
  )
end

#idObject



44
45
46
# File 'lib/passive_record/associations/has_one.rb', line 44

def id
  parent_model.id
end

#lookupObject



20
21
22
# File 'lib/passive_record/associations/has_one.rb', line 20

def lookup
  child_class.find_by(parent_model_id_field => parent_model.id)
end

#parent_class_nameObject



36
37
38
# File 'lib/passive_record/associations/has_one.rb', line 36

def parent_class_name
  association.parent_class.name.split('::').last.underscore
end

#parent_model_id_fieldObject



32
33
34
# File 'lib/passive_record/associations/has_one.rb', line 32

def parent_model_id_field
  parent_class_name + "_id"
end