module Mongoid::Criteria::Modifiable
def build(attrs = {}, &block)
- Since: - 2.0.0
Returns:
-
(Document)- A non-persisted document.
Other tags:
- Example: Build with selectors getting ignored. -
Example: build the document. -
def build(attrs = {}, &block) create_document(:new, attrs, &block) end
def create(attrs = {}, &block)
- Since: - 2.0.0.rc.1
Returns:
-
(Document)- A newly created document.
Other tags:
- Example: Create with selectors getting ignored. -
Example: Create the document. -
def create(attrs = {}, &block) create_document(:create, attrs, &block) end
def create!(attrs = {}, &block)
- Since: - 3.0.0
Returns:
-
(Document)- A newly created document.
Raises:
-
(Errors::Validations)- on a validation error.
Other tags:
- Example: Create with selectors getting ignored. -
Example: Create the document. -
def create!(attrs = {}, &block) create_document(:create!, attrs, &block) end
def create_document(method, attrs = nil, &block)
- Since: - 3.0.0
Returns:
-
(Document)- The new or saved document.
Parameters:
-
attrs(Hash) -- Additional attributes to use. -
method(Symbol) -- Either :new or :create.
Other tags:
- Example: Create a new document. -
Other tags:
- Api: - private
def create_document(method, attrs = nil, &block) attributes = selector.reduce(attrs ? attrs.dup : {}) do |hash, (key, value)| unless key.to_s =~ /\$/ || value.is_a?(Hash) hash[key] = value end hash end if embedded? attributes[:_parent] = parent_document attributes[:__metadata] = metadata end klass.__send__(method, attributes, &block) end
def create_with(attrs = {})
- Since: - 5.1.0
Returns:
-
(Mongoid::Criteria)- A criteria.
Other tags:
- Example: Define attributes to be used when a new document is created. -
def create_with(attrs = {}) where(selector.merge(attrs)) end
def find_or(method, attrs = {}, &block)
-
(Document)- The first or new document.
Parameters:
-
attrs(Hash) -- The attributes to query or set. -
method(Symbol) -- The method to invoke.
Other tags:
- Example: Find or perform an action. -
Other tags:
- Api: - private
def find_or(method, attrs = {}, &block) where(attrs).first || send(method, attrs, &block) end
def find_or_create_by(attrs = {}, &block)
-
(Document)- A matching or newly created document.
Parameters:
-
attrs(Hash) -- The attributes to check.
Other tags:
- Example: Find or create the document. -
def find_or_create_by(attrs = {}, &block) find_or(:create, attrs, &block) end
def find_or_create_by!(attrs = {}, &block)
-
(Document)- A matching or newly created document.
Raises:
-
(Errors::Validations)- on validation error.
Parameters:
-
attrs(Hash) -- The attributes to check.
Other tags:
- Example: Find or create the document. -
def find_or_create_by!(attrs = {}, &block) find_or(:create!, attrs, &block) end
def find_or_initialize_by(attrs = {}, &block)
-
(Document)- A matching or newly initialized document.
Parameters:
-
attrs(Hash) -- The attributes to check.
Other tags:
- Example: Find or initialize the document. -
def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end
def first_or(method, attrs = {}, &block)
- Since: - 3.1.0
Returns:
-
(Document)- The first or new document.
Parameters:
-
attrs(Hash) -- The attributes to query or set. -
method(Symbol) -- The method to invoke.
Other tags:
- Example: First or perform an action. -
Other tags:
- Api: - private
def first_or(method, attrs = {}, &block) first || create_document(method, attrs, &block) end
def first_or_create(attrs = nil, &block)
- Since: - 3.1.0
Returns:
-
(Document)- A matching or newly created document.
Parameters:
-
attrs(Hash) -- The additional attributes to add.
Other tags:
- Example: First or create the document. -
def first_or_create(attrs = nil, &block) first_or(:create, attrs, &block) end
def first_or_create!(attrs = nil, &block)
- Since: - 3.1.0
Returns:
-
(Document)- A matching or newly created document.
Parameters:
-
attrs(Hash) -- The additional attributes to add.
Other tags:
- Example: First or create the document. -
def first_or_create!(attrs = nil, &block) first_or(:create!, attrs, &block) end
def first_or_initialize(attrs = nil, &block)
- Since: - 3.1.0
Returns:
-
(Document)- A matching or newly initialized document.
Parameters:
-
attrs(Hash) -- The additional attributes to add.
Other tags:
- Example: First or initialize the document. -
def first_or_initialize(attrs = nil, &block) first_or(:new, attrs, &block) end