Module: Mongoid::Commands::ClassMethods

Defined in:
lib/mongoid/commands.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = {}) ⇒ Object

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

Example:

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

Returns: the Document.



122
123
124
125
126
# File 'lib/mongoid/commands.rb', line 122

def create(attributes = {})
  document = new(attributes)
  Create.execute(document)
  document
end

#create!(attributes = {}) ⇒ Object

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

Example:

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

Returns: the Document.



137
138
139
140
141
# File 'lib/mongoid/commands.rb', line 137

def create!(attributes = {})
  document = Create.execute(new(attributes), true)
  raise Errors::Validations.new(document.errors) unless document.errors.empty?
  document
end

#delete_all(conditions = {}) ⇒ Object

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.

Example:

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

Returns: true or raises an error.



153
154
155
# File 'lib/mongoid/commands.rb', line 153

def delete_all(conditions = {})
  DeleteAll.execute(self, conditions)
end

#destroy_all(conditions = {}) ⇒ Object

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.

Example:

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

Returns: true or raises an error.



167
168
169
# File 'lib/mongoid/commands.rb', line 167

def destroy_all(conditions = {})
  DestroyAll.execute(self, conditions)
end