Class: Mongoid::Persistence::Atomic::Push

Inherits:
Operation show all
Defined in:
lib/mongoid/persistence/atomic/push.rb

Overview

This class provides the ability to perform an explicit $push modification on a specific field.

Instance Attribute Summary

Attributes inherited from Operation

#document, #field, #options, #value

Instance Method Summary collapse

Methods inherited from Operation

#initialize, #operation

Methods included from Safe

#safe_mode?

Constructor Details

This class inherits a constructor from Mongoid::Persistence::Atomic::Operation

Instance Method Details

#persistObject

Sends the atomic $push operation to the database.

Examples:

Persist the new values.

push.persist

Returns:

  • (Object)

    The new array value.

Since:

  • 2.0.0



18
19
20
21
22
23
24
# File 'lib/mongoid/persistence/atomic/push.rb', line 18

def persist
  document[field] = [] unless document[field]
  document.send(field).push(value).tap do |value|
    document.collection.update(document._selector, operation("$push"), options)
    document.changes.delete(field.to_s)
  end
end