Class: Mongoid::Persistence::InsertEmbedded

Inherits:
Command show all
Defined in:
lib/mongoid/persistence/insert_embedded.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.insert(
  { "_id" : 1, "field" : "value" },
  false
);

Instance Attribute Summary

Attributes inherited from Command

#collection, #document, #klass, #options, #selector, #validate

Instance Method Summary collapse

Methods inherited from Command

#initialize

Constructor Details

This class inherits a constructor from Mongoid::Persistence::Command

Instance Method Details

#persistObject

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.

Example:

Insert.persist

Returns:

The Document, whether the insert succeeded or not.



26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/persistence/insert_embedded.rb', line 26

def persist
  parent = @document._parent
  if parent.new_record?
    parent.insert
  else
    update = { @document._inserter => { @document._position => @document.raw_attributes } }
    @collection.update(parent._selector, update, @options.merge(:multi => false))
  end
  @document.new_record = false; @document
end