Module: Mongoid::Persistence::ClassMethods

Defined in:
lib/mongoid/persistence.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}, &block) ⇒ Document

Create a new document. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not.

Examples:

Create a new document.

Person.create(:title => "Mr")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to create with.

Returns:

  • (Document)

    The newly created document.



168
169
170
171
172
# File 'lib/mongoid/persistence.rb', line 168

def create(attributes = {}, &block)
  creating do
    new(attributes, &block).tap { |doc| doc.save }
  end
end

#create!(attributes = {}, &block) ⇒ Document

Create a new document. This will instantiate a new document and insert it in a single call. Will always return the document whether save passed or not, and if validation fails an error will be raise.

Examples:

Create a new document.

Person.create!(:title => "Mr")

Parameters:

  • attributes (Hash) (defaults to: {})

    The attributes to create with.

Returns:

  • (Document)

    The newly created document.



185
186
187
188
189
190
191
192
# File 'lib/mongoid/persistence.rb', line 185

def create!(attributes = {}, &block)
  creating do
    new(attributes, &block).tap do |doc|
      fail_validate!(doc) if doc.insert.errors.any?
      fail_callback!(doc, :create!) if doc.new?
    end
  end
end

#delete_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. Does not fire any callbacks.

Examples:

Delete matching documents from the collection.

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

Delete all documents from the collection.

Person.delete_all

Parameters:

  • conditions (Hash) (defaults to: nil)

    Optional conditions to delete by.

Returns:

  • (Integer)

    The number of documents deleted.



207
208
209
210
211
212
213
214
# File 'lib/mongoid/persistence.rb', line 207

def delete_all(conditions = nil)
  selector = (conditions || {})[:conditions] || {}
  selector.merge!(:_type => name) if hereditary?
  collection.find(selector).count.tap do
    collection.remove(selector, Safety.merge_safety_options)
    Threaded.clear_safety_options!
  end
end

#destroy_all(conditions = {}) ⇒ 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(:conditions => { :title => "Sir" })

Destroy all documents from the collection.

Person.destroy_all

Parameters:

  • conditions (Hash) (defaults to: {})

    Optional conditions to destroy by.

Returns:

  • (Integer)

    The number of documents destroyed.



229
230
231
232
233
234
# File 'lib/mongoid/persistence.rb', line 229

def destroy_all(conditions = {})
  documents = all(conditions)
  documents.count.tap do
    documents.each { |doc| doc.destroy }
  end
end

#fail_callback!(document, method) ⇒ Object

Raise an error if a callback failed.

Examples:

Raise the callback error.

Person.fail_callback!(person, :create!)

Parameters:

  • document (Document)

    The document to fail.

  • method (Symbol)

    The method being called.

Raises:

Since:

  • 2.2.0



255
256
257
# File 'lib/mongoid/persistence.rb', line 255

def fail_callback!(document, method)
  raise Errors::Callback.new(document.class, method)
end

#fail_validate!(document) ⇒ Object

Raise an error if validation failed.

Examples:

Raise the validation error.

Person.fail_validate!(person)

Parameters:

  • document (Document)

    The document to fail.

Raises:



242
243
244
# File 'lib/mongoid/persistence.rb', line 242

def fail_validate!(document)
  raise Errors::Validations.new(document)
end