Module: Mongoid::Persistable::Destroyable::ClassMethods

Defined in:
lib/mongoid/persistable/destroyable.rb

Overview

Since:

  • 4.0.0

Instance Method Summary collapse

Instance Method Details

#destroy_all(conditions = nil) ⇒ Integer

Delete all documents given the supplied conditions. If no conditions are passed, the entire collection will be dropped for performance benefits. Fires the destroy callbacks if conditions were passed.

Examples:

Destroy matching documents from the collection.

Person.destroy_all({ :title => "Sir" })

Destroy all documents from the collection.

Person.destroy_all

Parameters:

  • conditions (Hash) (defaults to: nil)

    Optional conditions to destroy by.

Returns:

  • (Integer)

    The number of documents destroyed.

Since:

  • 1.0.0



50
51
52
53
54
55
56
# File 'lib/mongoid/persistable/destroyable.rb', line 50

def destroy_all(conditions = nil)
  selector = conditions || {}
  documents = where(selector)
  destroyed = documents.count
  documents.each { |doc| doc.destroy }
  destroyed
end