Class: MongoModel::Associations::BelongsTo::Association

Inherits:
MongoModel::Associations::Base::Association show all
Defined in:
lib/mongomodel/concerns/associations/belongs_to.rb

Instance Attribute Summary

Attributes inherited from MongoModel::Associations::Base::Association

#definition, #instance

Instance Method Summary collapse

Methods inherited from MongoModel::Associations::Base::Association

#ensure_class, #initialize, #proxy

Constructor Details

This class inherits a constructor from MongoModel::Associations::Base::Association

Instance Method Details

#find_targetObject


68
69
70
# File 'lib/mongomodel/concerns/associations/belongs_to.rb', line 68

def find_target
  target_class.find(target_id) if target_id && target_class
end

#replace(obj) ⇒ Object


59
60
61
62
63
64
65
66
# File 'lib/mongomodel/concerns/associations/belongs_to.rb', line 59

def replace(obj)
  ensure_class(obj) if obj && !polymorphic?

  instance[foreign_key] = obj ? obj.id : nil
  instance[type_key] = obj ? obj.class : nil if polymorphic?

  super
end

#target_classObject


51
52
53
54
55
56
57
# File 'lib/mongomodel/concerns/associations/belongs_to.rb', line 51

def target_class
  if polymorphic?
    instance[type_key].constantize rescue nil
  else
    klass
  end
end

#target_idObject


47
48
49
# File 'lib/mongomodel/concerns/associations/belongs_to.rb', line 47

def target_id
  instance[foreign_key]
end