Module: Mongoid::Persistable::Creatable::ClassMethods
- Defined in:
- lib/mongoid/persistable/creatable.rb
Instance Method Summary collapse
-
#create(attributes = nil, &block) ⇒ Document+
Create a new document.
-
#create!(attributes = nil, &block) ⇒ Document+
Create a new document.
Instance Method Details
#create(attributes = nil, &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.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/mongoid/persistable/creatable.rb', line 130 def create(attributes = nil, &block) _creating do if attributes.is_a?(::Array) attributes.map { |attrs| create(attrs, &block) } else doc = new(attributes, &block) doc.save doc end end end |
#create!(attributes = nil, &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.
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mongoid/persistable/creatable.rb', line 157 def create!(attributes = nil, &block) _creating do if attributes.is_a?(::Array) attributes.map { |attrs| create!(attrs, &block) } else doc = new(attributes, &block) doc.fail_due_to_validation! unless doc.insert.errors.empty? doc.fail_due_to_callback!(:create!) if doc.new_record? doc end end end |