Class Mongoid::Relations::Referenced::ManyToMany
In: lib/mongoid/relations/referenced/many_to_many.rb
Parent: Many

This class defines the behaviour for all relations that are a many-to-many between documents in different collections.

Methods

<<   build   clear   concat   create   create!   delete   new   nullify   nullify_all   purge   push  

Public Instance methods

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.posts << post

@example Push a document.

  person.posts.push(post)

@example Concat with other documents.

  person.posts.concat([ post_one, post_two ])

@param [ Document, Array<Document> ] *args Any number of documents.

@return [ Array<Document> ] The loaded docs.

@since 2.0.0.beta.1

Build a new document from the attributes and append it to this relation without saving.

@example Build a new document on the relation.

  person.posts.build(:title => "A new post")

@overload build(attributes = {}, options = {}, type = nil)

  @param [ Hash ] attributes The attributes of the new document.
  @param [ Hash ] options The scoped assignment options.
  @param [ Class ] type The optional subclass to build.

@overload build(attributes = {}, type = nil)

  @param [ Hash ] attributes The attributes of the new document.
  @param [ Hash ] options The scoped assignment options.
  @param [ Class ] type The optional subclass to build.

@return [ Document ] The new document.

@since 2.0.0.beta.1

clear()

Alias for nullify

concat(*args)

Alias for #<<

Creates a new document on the references many relation. This will save the document if the parent has been persisted.

@example Create and save the new document.

  person.posts.create(:text => "Testing")

@param [ Hash ] attributes The attributes to create with. @param [ Class ] type The optional type of document to create.

@return [ Document ] The newly created document.

@since 2.0.0.beta.1

Creates a new document on the references many relation. This will save the document if the parent has been persisted and will raise an error if validation fails.

@example Create and save the new document.

  person.posts.create!(:text => "Testing")

@param [ Hash ] attributes The attributes to create with. @param [ Class ] type The optional type of document to create.

@raise [ Errors::Validations ] If validation failed.

@return [ Document ] The newly created document.

@since 2.0.0.beta.1

Delete the document from the relation. This will set the foreign key on the document to nil. If the dependent options on the relation are :delete or :destroy the appropriate removal will occur.

@example Delete the document.

  person.posts.delete(post)

@param [ Document ] document The document to remove.

@return [ Document ] The matching document.

@since 2.1.0

new(attributes = {}, options = {}, type = nil)

Alias for build

Removes all associations between the base document and the target documents by deleting the foreign keys and the references, orphaning the target documents in the process.

@example Nullify the relation.

  person.preferences.nullify

@since 2.0.0.rc.1

nullify_all()

Alias for nullify

purge()

Alias for nullify

push(*args)

Alias for #<<

[Validate]