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

Inherits:
Object
  • Object
show all
Includes:
Operation
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 included from Operation

#document, #field, #options, #value

Instance Method Summary collapse

Methods included from Operation

#collection, #initialize, #operation, #path, #prepare

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



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

def persist
  prepare do
    document[field] = [] unless document[field]
    document.send(field).push(value).tap do |value|
      collection.update(document.atomic_selector, operation("$push"), options)
      document.remove_change(field)
    end
  end
end