Parent

Methods

Class/Module Index [+]

Quicksearch

Mongoid::Persistence::Atomic::AddToSet

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

Public Instance Methods

persist() click to toggle source

Sends the atomic $addToSet operation to the database.

@example Persist the new values.

addToSet.persist

@return [ Object ] The new array value.

@since 2.0.0

# File lib/mongoid/persistence/atomic/add_to_set.rb, line 18
def persist
  prepare do
    document[field] = [] unless document[field]
    values = document.send(field)
    values.push(value) unless values.include?(value)
    values.tap do
      if document.persisted?
        collection.update(document.atomic_selector, operation("$addToSet"), options)
        document.remove_change(field)
      end
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.