Class: Mongoid::Associations::HasOneRelated

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/associations/has_one_related.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, options) ⇒ HasOneRelated

Initializing a related association only requires looking up the objects by their ids.

Options:

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



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

def initialize(document, options)
  @parent, @klass = document, options.klass
  @foreign_key = document.class.to_s.foreign_key
  @document = @klass.first(:conditions => { @foreign_key => @parent.id })
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.



41
42
43
# File 'lib/mongoid/associations/has_one_related.rb', line 41

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/has_one_related.rb', line 7

def document
  @document
end

#klassObject (readonly)

Returns the value of attribute klass.



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

def klass
  @klass
end

Class Method Details

.instantiate(document, options) ⇒ Object

Preferred method for creating the new RelatesToMany association.

Options:

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



52
53
54
# File 'lib/mongoid/associations/has_one_related.rb', line 52

def instantiate(document, options)
  new(document, options)
end

.macroObject

Returns the macro used to create the association.



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

def macro
  :has_one_related
end

.update(related, document, 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 to update. document: The parent Document. options: The association Options

Example:

HasManyToRelated.update(game, person, options)



73
74
75
76
77
# File 'lib/mongoid/associations/has_one_related.rb', line 73

def update(related, document, options)
  name = document.class.to_s.underscore
  related.send("#{name}=", document)
  related
end

Instance Method Details

#build(attributes = {}) ⇒ Object

Builds a new Document and sets it as the association.

Returns the newly created object.



12
13
14
15
16
17
# File 'lib/mongoid/associations/has_one_related.rb', line 12

def build(attributes = {})
  @document = @klass.instantiate(attributes)
  name = @parent.class.to_s.underscore
  @document.send("#{name}=", @parent)
  @document
end

#create(attributes) ⇒ Object

Builds a new Document and sets it as the association, then saves the newly created document.

Returns the newly created object.



23
24
25
# File 'lib/mongoid/associations/has_one_related.rb', line 23

def create(attributes)
  build(attributes); @document.save; @document
end