Method: Mongoid::Criteria::Modifiable#find_or_create_by

Defined in:
lib/mongoid/criteria/modifiable.rb

#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.



87
88
89
# File 'lib/mongoid/criteria/modifiable.rb', line 87

def find_or_create_by(attrs = {}, &block)
  find_or(:create, attrs, &block)
end