Class: Mongoid::Persistence::Atomic::AddToSet

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

Overview

This class provides the ability to perform an explicit $addToSet 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 $addToSet operation to the database.

Examples:

Persist the new values.

addToSet.persist

Returns:

  • (Object)

    The new array value.

Since:

  • 2.0.0



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

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