Module: Mongoid::Criterion::Creational

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

Overview

This module defines criteria behavior for creating documents in the database for specified conditions.

Instance Method Summary collapse

Instance Method Details

#create(attrs = {}) ⇒ 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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mongoid/criterion/creational.rb', line 21

def create(attrs = {})
  klass.create(
    selector.inject(attrs) do |hash, (key, value)|
      hash.tap do |attrs|
        unless key.to_s =~ /\$/ || value.is_a?(Hash)
          attrs[key] = value
        end
      end
    end
  )
end