Class: Mongoid::Associations::HasOneRelated
- Defined in:
- lib/mongoid/associations/has_one_related.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method for creating the new
RelatesToMany
association. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(related, 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) ⇒ HasOneRelated
constructor
Initializing a related association only requires looking up the objects by their ids.
-
#method_missing(name, *args) ⇒ Object
Delegate all missing methods over to the
Document
.
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, ) @parent, @klass = document, .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
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/mongoid/associations/has_one_related.rb', line 7 def document @document end |
#klass ⇒ Object (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, ) new(document, ) 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(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(, document, ) name = document.class.to_s.underscore .send("#{name}=", document) 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 |