Module: Mongoid::Persistence::ClassMethods

Defined in:
lib/mongoid/persistence.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#create(attributes = nil, options = {}, &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")

Create multiple new documents.

Person.create({ title: "Mr" }, { title: "Mrs" })

Parameters:

  • attributes (Hash, Array) (defaults to: nil)

    The attributes to create with, or an Array of multiple attributes for multiple documents.

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

    A mass-assignment protection options. Supports :as and :without_protection

Returns:

Since:

  • 1.0.0



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/mongoid/persistence.rb', line 239

def create(attributes = nil, options = {}, &block)
  _creating do
    if attributes.is_a?(::Array)
      attributes.map { |attrs| create(attrs, options, &block) }
    else
      doc = new(attributes, options, &block)
      doc.save
      doc
    end
  end
end

#create!(attributes = {}, options = {}, &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")

Create multiple new documents.

Person.create!({ title: "Mr" }, { title: "Mrs" })

Parameters:

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

    The attributes to create with, or an Array of multiple attributes for multiple documents.

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

    A mass-assignment protection options. Supports :as and :without_protection

Returns:

Since:

  • 1.0.0



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/mongoid/persistence.rb', line 270

def create!(attributes = {}, options = {}, &block)
  _creating do
    if attributes.is_a?(::Array)
      attributes.map { |attrs| create!(attrs, options, &block) }
    else
      doc = new(attributes, options, &block)
      fail_validate!(doc) unless doc.insert.errors.empty?
      fail_callback!(doc, :create!) if doc.new_record?
      doc
    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.



296
297
298
299
300
301
302
303
304
305
# File 'lib/mongoid/persistence.rb', line 296

def delete_all(conditions = nil)
  conds = conditions || {}
  selector = conds[:conditions] || conds
  selector.merge!(_type: name) if hereditary?
  coll = collection
  deleted = coll.find(selector).count
  coll.find(selector).remove_all
  Threaded.clear_options!
  deleted
end

#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(:conditions => { :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.



320
321
322
323
324
325
326
# File 'lib/mongoid/persistence.rb', line 320

def destroy_all(conditions = nil)
  conds = conditions || {}
  documents = where(conds[:conditions] || conds)
  destroyed = documents.count
  documents.each { |doc| doc.destroy }
  destroyed
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



347
348
349
# File 'lib/mongoid/persistence.rb', line 347

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:



334
335
336
# File 'lib/mongoid/persistence.rb', line 334

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