Module: Mongoid::Persistence::ClassMethods
- Defined in:
- lib/mongoid/persistence.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#create(attributes = {}) ⇒ Object
Create a new
Document
. -
#create!(attributes = {}) ⇒ Object
Create a new
Document
. -
#delete_all(conditions = {}) ⇒ Object
Delete all documents given the supplied conditions.
-
#destroy_all(conditions = {}) ⇒ Object
Delete all documents given the supplied conditions.
-
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
Instance Method Details
#create(attributes = {}) ⇒ Object
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.
Example:
Person.create(:title => "Mr")
Returns: the Document
.
162 163 164 |
# File 'lib/mongoid/persistence.rb', line 162 def create(attributes = {}) document = new(attributes); document.insert end |
#create!(attributes = {}) ⇒ Object
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.
Example:
Person.create!(:title => "Mr")
Returns: the Document
.
176 177 178 179 180 |
# File 'lib/mongoid/persistence.rb', line 176 def create!(attributes = {}) document = new(attributes) fail_validate!(document) if document.insert.errors.any? 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.
192 193 194 195 196 197 198 |
# File 'lib/mongoid/persistence.rb', line 192 def delete_all(conditions = {}) RemoveAll.new( self, false, conditions[:conditions] || {} ).persist 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.
210 211 212 213 214 |
# File 'lib/mongoid/persistence.rb', line 210 def destroy_all(conditions = {}) documents = all(conditions) count = documents.count documents.each { |doc| doc.destroy }; count end |
#fail_validate!(document) ⇒ Object
Raise an error if validation failed.
217 218 219 |
# File 'lib/mongoid/persistence.rb', line 217 def fail_validate!(document) raise Errors::Validations.new(document.errors) end |