Module: Mongoid::Criteria::Modifiable
- Included in:
- Mongoid::Criteria
- Defined in:
- lib/mongoid/criteria/modifiable.rb
Instance Attribute Summary collapse
- #create_attrs ⇒ Object readonly private
- #create_attrs Additional attributes to add to the Document upon creation.(AdditionalattributestoaddtotheDocumentuponcreation.) ⇒ Object readonly private
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 Attribute Details
#create_attrs ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/mongoid/criteria/modifiable.rb', line 8 def create_attrs @create_attrs end |
#create_attrs Additional attributes to add to the Document upon creation.(AdditionalattributestoaddtotheDocumentuponcreation.) ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 |
# File 'lib/mongoid/criteria/modifiable.rb', line 8 attr_reader :create_attrs |
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.
22 23 24 |
# File 'lib/mongoid/criteria/modifiable.rb', line 22 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.
39 40 41 |
# File 'lib/mongoid/criteria/modifiable.rb', line 39 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.
58 59 60 |
# File 'lib/mongoid/criteria/modifiable.rb', line 58 def create!(attrs = {}, &block) create_document(:create!, attrs, &block) end |
#create_with(attrs = {}) ⇒ Mongoid::Criteria
Define attributes with which new documents will be created.
Note that if ‘find_or_create_by` is called after this in a method chain, the attributes in the query will override those from this method.
73 74 75 76 77 |
# File 'lib/mongoid/criteria/modifiable.rb', line 73 def create_with(attrs = {}) tap do (@create_attrs ||= {}).merge!(attrs) end 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.
88 89 90 |
# File 'lib/mongoid/criteria/modifiable.rb', line 88 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.
104 105 106 |
# File 'lib/mongoid/criteria/modifiable.rb', line 104 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.
117 118 119 |
# File 'lib/mongoid/criteria/modifiable.rb', line 117 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.
132 133 134 |
# File 'lib/mongoid/criteria/modifiable.rb', line 132 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.
148 149 150 |
# File 'lib/mongoid/criteria/modifiable.rb', line 148 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.
163 164 165 |
# File 'lib/mongoid/criteria/modifiable.rb', line 163 def first_or_initialize(attrs = nil, &block) first_or(:new, attrs, &block) end |