lib/mongoid/finders.rb
# Find the first +Document+ given the conditions, or raises
# Mongoid::Errors::DocumentNotFound
#
# @example Find the document by attribute other than id
#   Person.find_by(:username => "superuser")
#
# @param [ Hash ] attrs The attributes to check.
#
# @raise [ Errors::DocumentNotFound ] If no document found.
#
# @return [ Document ] A matching document.
#
# @since 3.0.0
Find the first +Document+ given the conditions, or raises
Mongoid::Errors::DocumentNotFound
Find the first + Document + given the conditions or raises Mongoid :: Errors :: DocumentNotFound
def find_by(attrs = {})
      result = where(attrs).first
      if result.nil? && Mongoid.raise_not_found_error
        raise(Errors::DocumentNotFound.new(self, attrs))
      end
      yield(result) if result && block_given?
      result
    end
def find_by ( attrs = { } ) result = where ( attrs ) . first if result . nil? && Mongoid . raise_not_found_error raise ( Errors :: DocumentNotFound . new ( self , attrs ) ) end yield ( result ) if result && block_given? result end
Find the first +Document+ given the conditions, or raises
Mongoid::Errors::DocumentNotFound
Find the first + Document + given the conditions or raises Mongoid :: Errors :: DocumentNotFound
A matching document.
A matching document .
 Document 
[ Errors::DocumentNotFound ] If no document found.
[ Errors :: DocumentNotFound ] If no document found .
attrs
<not_specific>
<not_specific>
attrs The attributes to check.
attrs The attributes to check .
example
Find the document by attribute other than id Person.find_by(:username => "superuser")
Find the document by attribute other than id Person . find_by ( : username = > " superuser " )
since
None