Class: Mongoid::Associations::HasOneRelated

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

Overview

Represents an relational one-to-one association with an object in a separate collection or database.

Instance Attribute Summary

Attributes inherited from Proxy

#options, #target

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Proxy

#extends, #method_missing

Constructor Details

#initialize(document, options, target = nil) ⇒ 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.



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

def initialize(document, options, target = nil)
  @parent, @klass = document, options.klass
  @foreign_key = options.foreign_key
  @target = target || @klass.first(:conditions => { @foreign_key => @parent.id })
  extends(options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mongoid::Associations::Proxy

Class Method Details

.instantiate(document, options, target = nil) ⇒ 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, target = nil)
  new(document, options, target)
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(target, 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:

HasOneToRelated.update(game, person, options)



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

def update(target, document, options)
  if target
    name = document.class.to_s.underscore
    target.send("#{name}=", document)
    return instantiate(document, options, target)
  end
  target
end

Instance Method Details

#build(attributes = {}) ⇒ Object

Builds a new Document and sets it as the association.

Returns the newly created object.



13
14
15
16
17
18
19
20
21
# File 'lib/mongoid/associations/has_one_related.rb', line 13

def build(attributes = {})
  @target = @klass.instantiate(attributes)
  inverse = @target.associations.values.detect do ||
    .options.klass == @parent.class
  end
  name = inverse.name
  @target.send("#{name}=", @parent)
  @target
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.



27
28
29
# File 'lib/mongoid/associations/has_one_related.rb', line 27

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