Class/Module Index [+]

Quicksearch

Mongoid::Relations::Referenced::ManyToMany

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

Public Instance Methods

<<(*args) click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 26
def <<(*args)
  batched do
    [].tap do |ids|
      args.flatten.each do |doc|
        next unless doc
        append(doc)
        if persistable? || _creating?
          ids.push(doc.id)
          doc.save
        else
          base.send(metadata.foreign_key).push(doc.id)
          base.synced[metadata.foreign_key] = false
        end
      end
      if persistable? || _creating?
        base.push_all(metadata.foreign_key, ids)
        base.synced[metadata.foreign_key] = false
      end
    end
  end
end
Also aliased as: concat, push
build(attributes = {}, options = {}, type = nil) click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 69
def build(attributes = {}, options = {}, type = nil)
  if options.is_a? Class
    options, type = {}, options
  end

  Factory.build(type || klass, attributes, options).tap do |doc|
    base.send(metadata.foreign_key).push(doc.id)
    append(doc)
    yield(doc) if block_given?
  end
end
Also aliased as: new
clear() click to toggle source
Alias for: nullify
concat(*args) click to toggle source
Alias for: <<
create(attributes = nil, type = nil, &block) click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 94
def create(attributes = nil, type = nil, &block)
  super.tap do |doc|
    base.send(metadata.foreign_key).delete_one(doc.id)
    base.push(metadata.foreign_key, doc.id)
    base.synced[metadata.foreign_key] = false
  end
end
create!(attributes = nil, type = nil, &block) click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 117
def create!(attributes = nil, type = nil, &block)
  super.tap do |doc|
    base.send(metadata.foreign_key).delete_one(doc.id)
    base.push(metadata.foreign_key, doc.id)
    base.synced[metadata.foreign_key] = false
  end
end
delete(document) click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 137
def delete(document)
  super.tap do |doc|
    if doc && persistable?
      base.pull(metadata.foreign_key, doc.id)
      base.synced[metadata.foreign_key] = false
    end
  end
end
new(attributes = {}, options = {}, type = nil) click to toggle source
Alias for: build
nullify() click to toggle source

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

# File lib/mongoid/relations/referenced/many_to_many.rb, line 154
def nullify
  unless metadata.forced_nil_inverse?
    criteria.pull(metadata.inverse_foreign_key, base.id)
  end
  if persistable?
    base.set(
      metadata.foreign_key,
      base.send(metadata.foreign_key).clear
    )
  end
  target.clear do |doc|
    unbind_one(doc)
  end
end
Also aliased as: nullify_all, clear, purge
nullify_all() click to toggle source
Alias for: nullify
purge() click to toggle source
Alias for: nullify
push(*args) click to toggle source
Alias for: <<

[Validate]

Generated with the Darkfish Rdoc Generator 2.