Class: Mongoid::Associations::BelongsToRelated
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/belongs_to_related.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Instantiate a new
BelongsToRelated
or return nil if the foreign key is nil. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(related, parent, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#initialize(document, foreign_key, options) ⇒ BelongsToRelated
constructor
Initializing a related association only requires looking up the object by its id.
-
#method_missing(name, *args) ⇒ Object
Delegate all missing methods over to the
Document
.
Methods included from Proxy
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, ) @document = .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
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/mongoid/associations/belongs_to_related.rb', line 7 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/mongoid/associations/belongs_to_related.rb', line 7 def @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, ) foreign_key = document.send(.foreign_key) foreign_key.blank? ? nil : new(document, foreign_key, ) end |
.macro ⇒ Object
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(, parent, ) parent.send("#{.foreign_key}=", .id) if end |