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, #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. This delegates to the standard MongoDB collection’s insert command.

Example:

Insert.persist

Returns:

The Document, whether the insert succeeded or not.



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

def persist
  return @document if @validate && !@document.valid?
  @document.run_callbacks(:before_create)
  @document.run_callbacks(:before_save)
  if insert
    @document.new_record = false
    # TODO: All child document new_record flags must get set to false
    # here or somewhere - this will cause problems.
    @document.move_changes
    @document.run_callbacks(:after_create)
    @document.run_callbacks(:after_save)
  end
  @document
end