module Mongoid::Criterion::Modifiable

Public Instance Methods

build(attrs = {}, &block) click to toggle source

Build a document given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.

@example build the document.

Person.where(:title => "Sir").build

@example Build with selectors getting ignored.

Person.where(:age.gt => 5).build

@return [ Document ] A non-persisted document.

@since 2.0.0

# File lib/mongoid/criterion/modifiable.rb, line 17
def build(attrs = {}, &block)
  create_document(:new, attrs, &block)
end
Also aliased as: new
create(attrs = {}, &block) click to toggle source

Create a document in the database given the selector and return it. Complex criteria, such as $in and $or operations will get ignored.

@example Create the document.

Person.where(:title => "Sir").create

@example Create with selectors getting ignored.

Person.where(:age.gt => 5).create

@return [ Document ] A newly created document.

@since 2.0.0.rc.1

# File lib/mongoid/criterion/modifiable.rb, line 34
def create(attrs = {}, &block)
  create_document(:create, attrs, &block)
end
create!(attrs = {}, &block) click to toggle source

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.

@example Create the document.

Person.where(:title => "Sir").create

@example Create with selectors getting ignored.

Person.where(:age.gt => 5).create

@raise [ Errors::Validations ] on a validation error.

@return [ Document ] A newly created document.

@since 3.0.0

# File lib/mongoid/criterion/modifiable.rb, line 53
def create!(attrs = {}, &block)
  create_document(:create!, attrs, &block)
end
find_or_create_by(attrs = {}, &block) click to toggle source

Find the first Document given the conditions, or creates a new document with the conditions that were supplied.

@example Find or create the document.

Person.find_or_create_by(:attribute => "value")

@param [ Hash ] attrs The attributes to check.

@return [ Document ] A matching or newly created document.

# File lib/mongoid/criterion/modifiable.rb, line 66
def find_or_create_by(attrs = {}, &block)
  find_or(:create, attrs, &block)
end
find_or_initialize_by(attrs = {}, &block) click to toggle source

Find the first Document given the conditions, or initializes a new document with the conditions that were supplied.

@example Find or initialize the document.

Person.find_or_initialize_by(:attribute => "value")

@param [ Hash ] attrs The attributes to check.

@return [ Document ] A matching or newly initialized document.

# File lib/mongoid/criterion/modifiable.rb, line 79
def find_or_initialize_by(attrs = {}, &block)
  find_or(:new, attrs, &block)
end
first_or_create(attrs = nil, &block) click to toggle source

Find the first Document, or creates a new document with the conditions that were supplied plus attributes.

@example First or create the document.

Person.where(name: "Jon").first_or_create(attribute: "value")

@param [ Hash ] attrs The additional attributes to add.

@return [ Document ] A matching or newly created document.

@since 3.1.0

# File lib/mongoid/criterion/modifiable.rb, line 94
def first_or_create(attrs = nil, &block)
  first_or(:create, attrs, &block)
end
first_or_create!(attrs = nil, &block) click to toggle source

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.

@example First or create the document.

Person.where(name: "Jon").first_or_create!(attribute: "value")

@param [ Hash ] attrs The additional attributes to add.

@return [ Document ] A matching or newly created document.

@since 3.1.0

# File lib/mongoid/criterion/modifiable.rb, line 110
def first_or_create!(attrs = nil, &block)
  first_or(:create!, attrs, &block)
end
first_or_initialize(attrs = nil, &block) click to toggle source

Find the first Document, or initializes a new document with the conditions that were supplied plus attributes.

@example First or initialize the document.

Person.where(name: "Jon").first_or_initialize(attribute: "value")

@param [ Hash ] attrs The additional attributes to add.

@return [ Document ] A matching or newly initialized document.

@since 3.1.0

# File lib/mongoid/criterion/modifiable.rb, line 125
def first_or_initialize(attrs = nil, &block)
  first_or(:new, attrs, &block)
end
new(attrs = {}, &block) click to toggle source
Alias for: build