Class: Mongoid::Associations::HasManyRelated
- Defined in:
- lib/mongoid/associations/has_many_related.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Preferred method for creating the new
HasManyRelated
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
-
#<<(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#build(attributes) ⇒ Object
Builds a new Document and adds it to the association collection.
-
#concat(*objects) ⇒ Object
Delegates to <<.
-
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection.
-
#initialize(document, options) ⇒ HasManyRelated
constructor
Initializing a related association only requires looking up the objects by their ids.
-
#push(*objects) ⇒ Object
Delegates to <<.
Methods included from Extensions::Array::Parentization
Methods included from Extensions::Array::Conversions
Methods included from Extensions::Array::Assimilation
Methods included from Extensions::Array::Accessors
Constructor Details
#initialize(document, options) ⇒ HasManyRelated
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
.
52 53 54 55 56 57 |
# File 'lib/mongoid/associations/has_many_related.rb', line 52 def initialize(document, ) @parent, @klass = document, .klass @foreign_key = document.class.to_s.foreign_key @documents = @klass.all(:conditions => { @foreign_key => document.id }) super(@documents) end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
6 7 8 |
# File 'lib/mongoid/associations/has_many_related.rb', line 6 def klass @klass end |
Class Method Details
.instantiate(document, options) ⇒ Object
Preferred method for creating the new HasManyRelated
association.
Options:
document: The Document
that contains the relationship. options: The association Options
.
71 72 73 |
# File 'lib/mongoid/associations/has_many_related.rb', line 71 def instantiate(document, ) new(document, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
76 77 78 |
# File 'lib/mongoid/associations/has_many_related.rb', line 76 def macro :has_many_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 parent: The parent Document
to update. options: The association Options
Example:
RelatesToOne.update(game, person, options)
92 93 94 95 |
# File 'lib/mongoid/associations/has_many_related.rb', line 92 def update(, document, ) name = document.class.to_s.underscore .each { |child| child.send("#{name}=", document) } end |
Instance Method Details
#<<(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
10 11 12 13 14 15 16 |
# File 'lib/mongoid/associations/has_many_related.rb', line 10 def <<(*objects) objects.flatten.each do |object| object.send("#{@foreign_key}=", @parent.id) @documents << object object.save unless @parent.new_record? end end |
#build(attributes) ⇒ Object
Builds a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor.
Returns the newly created object.
23 24 25 26 27 |
# File 'lib/mongoid/associations/has_many_related.rb', line 23 def build(attributes) object = @klass.instantiate(attributes.merge(@foreign_key => @parent.id)) @documents << object object end |
#concat(*objects) ⇒ Object
Delegates to <<
30 31 32 |
# File 'lib/mongoid/associations/has_many_related.rb', line 30 def concat(*objects) self << objects end |
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor and the new object will then be saved.
Returns the newly created object.
40 41 42 43 |
# File 'lib/mongoid/associations/has_many_related.rb', line 40 def create(attributes) object = build(attributes) object.save end |
#push(*objects) ⇒ Object
Delegates to <<
60 61 62 |
# File 'lib/mongoid/associations/has_many_related.rb', line 60 def push(*objects) self << objects end |