Parent

Methods

Class/Module Index [+]

Quicksearch

Mongoid::Persistence::Operations::Update

Update is a persistence command responsible for taking a document that has already been saved to the database and saving it, depending on whether or not the document has been modified.

Before persisting the command will check via dirty attributes if the document has changed, if not, it will simply return true. If it has it will go through the validation steps, run callbacks, and set the changed fields atomically on the document. The underlying query resembles the following MongoDB query:

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

For embedded documents it will use the positional locator:

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

Public Instance Methods

persist() click to toggle source

Persist the document that is to be updated to the database. This will only write changed fields via MongoDB’s $set modifier operation.

@example Update the document.

Update.persist

@return [ true, false ] If the save passed.

# File lib/mongoid/persistence/operations/update.rb, line 41
def persist
  prepare do
    unless updates.empty?
      collection.update(selector, updates, options)
      conflicts.each_pair do |key, value|
        collection.update(selector, { key => value }, options)
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.