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.
-
#find(id) ⇒ Object
Finds a document in this association.
-
#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
.
60 61 62 63 64 65 |
# File 'lib/mongoid/associations/has_many_related.rb', line 60 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
.
79 80 81 |
# File 'lib/mongoid/associations/has_many_related.rb', line 79 def instantiate(document, ) new(document, ) end |
.macro ⇒ Object
Returns the macro used to create the association.
84 85 86 |
# File 'lib/mongoid/associations/has_many_related.rb', line 84 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)
100 101 102 103 |
# File 'lib/mongoid/associations/has_many_related.rb', line 100 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 28 |
# File 'lib/mongoid/associations/has_many_related.rb', line 23 def build(attributes = {}) name = @parent.class.to_s.underscore object = @klass.instantiate(attributes.merge(name => @parent)) @documents << object object end |
#concat(*objects) ⇒ Object
Delegates to <<
31 32 33 |
# File 'lib/mongoid/associations/has_many_related.rb', line 31 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.
41 42 43 44 45 |
# File 'lib/mongoid/associations/has_many_related.rb', line 41 def create(attributes) object = build(attributes) object.save object end |
#find(id) ⇒ Object
Finds a document in this association. If an id is passed, will return the document for that id.
49 50 51 |
# File 'lib/mongoid/associations/has_many_related.rb', line 49 def find(id) @klass.find(id) end |
#push(*objects) ⇒ Object
Delegates to <<
68 69 70 |
# File 'lib/mongoid/associations/has_many_related.rb', line 68 def push(*objects) self << objects end |