Class | Mongoid::Relations::Embedded::Many |
In: |
lib/mongoid/relations/embedded/many.rb
|
Parent: | Relations::Many |
This class handles the behaviour for a document that embeds many other documents within in it as an array.
Instantiate a new embeds_many relation.
@example Create the new relation.
Many.new(person, addresses, metadata)
@param [ Document ] base The document this relation hangs off of. @param [ Array<Document> ] target The child documents of the relation. @param [ Metadata ] metadata The relation‘s metadata
@return [ Many ] The proxy.
Appends a document or array of documents to the relation. Will set the parent and update the index in the process.
@example Append a document.
person.addresses << address
@example Push a document.
person.addresses.push(address)
@example Concat with other documents.
person.addresses.concat([ address_one, address_two ])
@param [ Document, Array<Document> ] *args Any number of documents.
Builds a new document in the relation and appends it to the target. Takes an optional type if you want to specify a subclass.
@example Build a new document on the relation.
person.people.build(:name => "Bozo")
@overload build(attributes = {}, options = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Hash ] options The scoped assignment options. @param [ Class ] type Optional class to build the document with.
@overload build(attributes = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Class ] type Optional class to build the document with.
Returns a count of the number of documents in the association that have actually been persisted to the database.
Use size if you want the total number of documents.
@example Get the count of persisted documents.
person.addresses.count
@return [ Integer ] The total number of persisted embedded docs, as
flagged by the #persisted? method.
Create a new document in the relation. This is essentially the same as doing a build then save on the new document.
@example Create a new document in the relation.
person.movies.create(:name => "Bozo")
@overload create(attributes = {}, options = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Hash ] options The scoped assignment options. @param [ Class ] type Optional class to create the document with.
@overload create(attributes = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Class ] type Optional class to create the document with.
@return [ Document ] The newly created document.
Create a new document in the relation. This is essentially the same as doing a build then save on the new document. If validation failed on the document an error will get raised.
@example Create the document.
person.addresses.create!(:street => "Unter der Linden")</tt>
@overload create!(attributes = {}, options = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Hash ] options The scoped assignment options. @param [ Class ] type Optional class to create the document with.
@overload create!(attributes = {}, type = nil)
@param [ Hash ] attributes The attributes to build the document with. @param [ Class ] type Optional class to create the document with.
@raise [ Errors::Validations ] If a validation error occured.
@return [ Document ] The newly created document.
Delete the supplied document from the target. This method is proxied in order to reindex the array after the operation occurs.
@example Delete the document from the relation.
person.addresses.delete(address)
@param [ Document ] document The document to be deleted.
@return [ Document, nil ] The deleted document or nil if nothing deleted.
@since 2.0.0.rc.1
Delete all the documents in the association without running callbacks.
@example Delete all documents from the relation.
person.addresses.delete_all
@example Conditionally delete documents from the relation.
person.addresses.delete_all(:conditions => { :street => "Bond" })
@param [ Hash ] conditions Conditions on which documents to delete.
@return [ Integer ] The number of documents deleted.
Destroy all the documents in the association whilst running callbacks.
@example Destroy all documents from the relation.
person.addresses.destroy_all
@example Conditionally destroy documents from the relation.
person.addresses.destroy_all(:conditions => { :street => "Bond" })
@param [ Hash ] conditions Conditions on which documents to destroy.
@return [ Integer ] The number of documents destroyed.
Finds a document in this association through several different methods.
@example Find a document by its id.
person.addresses.find(BSON::ObjectId.new)
@example Find documents for multiple ids.
person.addresses.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
@example Find documents based on conditions.
person.addresses.find(:all, :conditions => { :number => 10 }) person.addresses.find(:first, :conditions => { :number => 10 }) person.addresses.find(:last, :conditions => { :number => 10 })
@param [ Array<Object> ] args Various arguments.
@return [ Array<Document>, Document ] A single or multiple documents.
Substitutes the supplied target documents for the existing documents in the relation.
@example Substitute the relation‘s target.
person.addresses.substitute([ address ])
@param [ Array<Document> ] new_target The replacement array. @param [ true, false ] building Are we in build mode?
@return [ Many ] The proxied relation.
@since 2.0.0.rc.1