Class: Mongoid::Associations::HasOneRelated
- 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
Class Method Summary collapse
-
.instantiate(document, options, target = nil) ⇒ Object
Preferred method for creating the new
RelatesToMany
association. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(target, document, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Object
Builds a new Document and sets it as the association.
-
#create(attributes) ⇒ Object
Builds a new Document and sets it as the association, then saves the newly created document.
-
#initialize(document, options, target = nil) ⇒ HasOneRelated
constructor
Initializing a related association only requires looking up the objects by their ids.
Methods inherited from Proxy
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, , target = nil) @parent, @klass = document, .klass @foreign_key = .foreign_key @target = target || @klass.first(:conditions => { @foreign_key => @parent.id }) extends() 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, , target = nil) new(document, , target) end |
.macro ⇒ Object
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, ) if target name = document.class.to_s.underscore target.send("#{name}=", document) return instantiate(document, , 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 || ..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 |