Class: Mongoid::Persistence::Operations::Embedded::Insert

Inherits:
Object
  • Object
show all
Includes:
Insertion, Mongoid::Persistence::Operations
Defined in:
lib/mongoid/persistence/operations/embedded/insert.rb

Overview

Insert is a persistence command responsible for taking a document that has not been saved to the database and saving it. This specific class handles the case when the document is embedded in another.

The underlying query resembles the following MongoDB query:

collection.update(
  { "_id" : 1 },
  { "$push" : { "field" : "value" } },
  false
);

Instance Attribute Summary

Attributes included from Mongoid::Persistence::Operations

#conflicts, #document

Instance Method Summary collapse

Methods included from Insertion

#prepare

Methods included from Mongoid::Persistence::Operations

#collection, #deletes, #initialize, insert, #inserts, #notifying_parent?, #options, #parent, remove, #selector, update, #updates, #validating?

Instance Method Details

#persistDocument

Insert the new document in the database. If the document’s parent is a new record, we will call save on the parent, otherwise we will $push the document onto the parent.

Examples:

Insert an embedded document.

Insert.persist

Returns:

  • (Document)

    The document to be inserted.



29
30
31
32
33
34
35
36
37
# File 'lib/mongoid/persistence/operations/embedded/insert.rb', line 29

def persist
  prepare do
    if parent.new?
      parent.insert
    else
      collection.update(parent.atomic_selector, inserts, options)
    end
  end
end