Module: Mongoid::Criterion::Modifiable

Included in:
Mongoid::Criteria
Defined in:
lib/mongoid/criterion/modifiable.rb

Instance Method Summary collapse

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.

Examples:

build the document.

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

Build with selectors getting ignored.

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

Returns:

  • (Document)

    A non-persisted document.

Since:

  • 2.0.0



18
19
20
# File 'lib/mongoid/criterion/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.

Examples:

Create the document.

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

Create with selectors getting ignored.

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

Returns:

  • (Document)

    A newly created document.

Since:

  • 2.0.0.rc.1



35
36
37
# File 'lib/mongoid/criterion/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.

Examples:

Create the document.

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

Create with selectors getting ignored.

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

Returns:

  • (Document)

    A newly created document.

Raises:

Since:

  • 3.0.0



54
55
56
# File 'lib/mongoid/criterion/modifiable.rb', line 54

def create!(attrs = {}, &block)
  create_document(: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.

Examples:

Find or create the document.

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

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Returns:

  • (Document)

    A matching or newly created document.



67
68
69
# File 'lib/mongoid/criterion/modifiable.rb', line 67

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.

Examples:

Find or initialize the document.

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

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes to check.

Returns:

  • (Document)

    A matching or newly initialized document.



80
81
82
# File 'lib/mongoid/criterion/modifiable.rb', line 80

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.

Examples:

First or create the document.

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

Parameters:

  • attrs (Hash) (defaults to: nil)

    The additional attributes to add.

Returns:

  • (Document)

    A matching or newly created document.

Since:

  • 3.1.0



95
96
97
# File 'lib/mongoid/criterion/modifiable.rb', line 95

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.

Examples:

First or create the document.

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

Parameters:

  • attrs (Hash) (defaults to: nil)

    The additional attributes to add.

Returns:

  • (Document)

    A matching or newly created document.

Since:

  • 3.1.0



111
112
113
# File 'lib/mongoid/criterion/modifiable.rb', line 111

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.

Examples:

First or initialize the document.

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

Parameters:

  • attrs (Hash) (defaults to: nil)

    The additional attributes to add.

Returns:

  • (Document)

    A matching or newly initialized document.

Since:

  • 3.1.0



126
127
128
# File 'lib/mongoid/criterion/modifiable.rb', line 126

def first_or_initialize(attrs = nil, &block)
  first_or(:new, attrs, &block)
end