Module: Mongoid::Criteria::Modifiable
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criteria/modifiable.rb
Instance Method Summary collapse
-
#build(attrs = {}, &block) ⇒ Document
(also: #new)
Build a document given the selector and return it.
-
#create(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it.
-
#create!(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it.
-
#create_with(attrs = {}) ⇒ Mongoid::Criteria
Define attributes with which new documents will be created.
-
#find_or_create_by(attrs = {}, &block) ⇒ Document
Find the first
Document
given the conditions, or creates a new document with the conditions that were supplied. -
#find_or_create_by!(attrs = {}, &block) ⇒ Document
Find the first
Document
given the conditions, or creates a new document with the conditions that were supplied. -
#find_or_initialize_by(attrs = {}, &block) ⇒ Document
Find the first
Document
given the conditions, or initializes a new document with the conditions that were supplied. -
#first_or_create(attrs = nil, &block) ⇒ Document
Find the first
Document
, or creates a new document with the conditions that were supplied plus attributes. -
#first_or_create!(attrs = nil, &block) ⇒ Document
Find the first
Document
, or creates a new document with the conditions that were supplied plus attributes and will raise an error if validation fails. -
#first_or_initialize(attrs = nil, &block) ⇒ Document
Find the first
Document
, or initializes a new document with the conditions that were supplied plus attributes.
Instance Method Details
#build(attrs = {}, &block) ⇒ Document Also known as: new
Build a document given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
18 19 20 |
# File 'lib/mongoid/criteria/modifiable.rb', line 18 def build(attrs = {}, &block) create_document(:new, attrs, &block) end |
#create(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.
35 36 37 |
# File 'lib/mongoid/criteria/modifiable.rb', line 35 def create(attrs = {}, &block) create_document(:create, attrs, &block) end |
#create!(attrs = {}, &block) ⇒ Document
Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored. If validation fails, an error will be raised.
54 55 56 |
# File 'lib/mongoid/criteria/modifiable.rb', line 54 def create!(attrs = {}, &block) create_document(:create!, attrs, &block) end |
#create_with(attrs = {}) ⇒ Mongoid::Criteria
Define attributes with which new documents will be created.
66 67 68 |
# File 'lib/mongoid/criteria/modifiable.rb', line 66 def create_with(attrs = {}) where(selector.merge(attrs)) end |
#find_or_create_by(attrs = {}, &block) ⇒ Document
Find the first Document
given the conditions, or creates a new document with the conditions that were supplied.
79 80 81 |
# File 'lib/mongoid/criteria/modifiable.rb', line 79 def find_or_create_by(attrs = {}, &block) find_or(:create, attrs, &block) end |
#find_or_create_by!(attrs = {}, &block) ⇒ Document
Find the first Document
given the conditions, or creates a new document with the conditions that were supplied. If validation fails an exception will be raised.
95 96 97 |
# File 'lib/mongoid/criteria/modifiable.rb', line 95 def find_or_create_by!(attrs = {}, &block) find_or(:create!, attrs, &block) end |
#find_or_initialize_by(attrs = {}, &block) ⇒ Document
Find the first Document
given the conditions, or initializes a new document with the conditions that were supplied.
108 109 110 |
# File 'lib/mongoid/criteria/modifiable.rb', line 108 def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end |
#first_or_create(attrs = nil, &block) ⇒ Document
Find the first Document
, or creates a new document with the conditions that were supplied plus attributes.
123 124 125 |
# File 'lib/mongoid/criteria/modifiable.rb', line 123 def first_or_create(attrs = nil, &block) first_or(:create, attrs, &block) end |
#first_or_create!(attrs = nil, &block) ⇒ Document
Find the first Document
, or creates a new document with the conditions that were supplied plus attributes and will raise an error if validation fails.
139 140 141 |
# File 'lib/mongoid/criteria/modifiable.rb', line 139 def first_or_create!(attrs = nil, &block) first_or(:create!, attrs, &block) end |
#first_or_initialize(attrs = nil, &block) ⇒ Document
Find the first Document
, or initializes a new document with the conditions that were supplied plus attributes.
154 155 156 |
# File 'lib/mongoid/criteria/modifiable.rb', line 154 def first_or_initialize(attrs = nil, &block) first_or(:new, attrs, &block) end |