Class: Mongoid::Associations::BelongsToRelated

Inherits:
Object
  • Object
show all
Includes:
Proxy
Defined in:
lib/mongoid/associations/belongs_to_related.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

Constructor Details

#initialize(document, foreign_key, options) ⇒ BelongsToRelated

Initializing a related association only requires looking up the object by its id.

Options:

document: The Document that contains the relationship. options: The association Options.



16
17
18
# File 'lib/mongoid/associations/belongs_to_related.rb', line 16

def initialize(document, foreign_key, options)
  @document = options.klass.find(foreign_key)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Delegate all missing methods over to the Document.



21
22
23
# File 'lib/mongoid/associations/belongs_to_related.rb', line 21

def method_missing(name, *args)
  @document.send(name, *args)
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/mongoid/associations/belongs_to_related.rb', line 7

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/mongoid/associations/belongs_to_related.rb', line 7

def options
  @options
end

Class Method Details

.instantiate(document, options) ⇒ Object

Instantiate a new BelongsToRelated or return nil if the foreign key is nil. It is preferrable to use this method over the traditional call to new.

Options:

document: The Document that contains the relationship. options: The association Options.



34
35
36
37
# File 'lib/mongoid/associations/belongs_to_related.rb', line 34

def instantiate(document, options)
  foreign_key = document.send(options.foreign_key)
  foreign_key.blank? ? nil : new(document, foreign_key, options)
end

.macroObject

Returns the macro used to create the association.



40
41
42
# File 'lib/mongoid/associations/belongs_to_related.rb', line 40

def macro
  :belongs_to_related
end

.update(related, parent, options) ⇒ Object

Perform an update of the relationship of the parent and child. This will assimilate the child Document into the parent’s object graph.

Options:

related: The related object parent: The parent Document to update. options: The association Options

Example:

BelongsToRelated.update(game, person, options)



56
57
58
59
# File 'lib/mongoid/associations/belongs_to_related.rb', line 56

def update(related, parent, options)
  parent.send("#{options.foreign_key}=", related.id) if related
  related
end