Class: Mongoid::Persistence::Insert

Inherits:
Command show all
Defined in:
lib/mongoid/persistence/insert.rb

Overview

Insert is a persistence command responsible for taking a document that has not been saved to the database and saving it.

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, #suppress, #validate

Instance Method Summary collapse

Methods inherited from Command

#initialize

Methods included from Safe

#safe_mode?

Constructor Details

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

Instance Method Details

#persistObject

Insert the new document in the database. This delegates to the standard MongoDB collection’s insert command.

Example:

Insert.persist

Returns:

The Document, whether the insert succeeded or not.



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

def persist
  return document if validate && document.invalid?(:create)
  document.run_callbacks(:save) do
    document.run_callbacks(:create) do
      if insert
        document.new_record = false
        document._children.each { |child| child.new_record = false }
        document.move_changes
      end
    end
  end; document
end