Module: Mongoid::Findable
- Extended by:
- Criteria::Queryable::Forwardable
- Defined in:
- build/mongoid-6.1/lib/mongoid/findable.rb
Overview
This module defines the finder methods that hang off the document at the class level.
Instance Method Summary collapse
-
#count ⇒ Integer
Returns a count of records in the database.
-
#empty? ⇒ true, false
Returns true if count is zero.
-
#exists? ⇒ true, false
Returns true if there are on document in database based on the provided arguments.
-
#find(*args) ⇒ Document, ...
Find a
Document
in several different ways. -
#find_by(attrs = {}) {|result| ... } ⇒ Document?
Find the first
Document
given the conditions. -
#find_by!(attrs = {}) {|result| ... } ⇒ Document
Find the first
Document
given the conditions, or raises Mongoid::Errors::DocumentNotFound. -
#first ⇒ Document
(also: #one)
Find the first
Document
given the conditions. -
#last ⇒ Document
Find the last
Document
given the conditions.
Methods included from Criteria::Queryable::Forwardable
Instance Method Details
#count ⇒ Integer
Returns a count of records in the database. If you want to specify conditions use where.
54 55 56 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 54 def count with_default_scope.count end |
#empty? ⇒ true, false
Returns true if count is zero
64 65 66 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 64 def empty? count == 0 end |
#exists? ⇒ true, false
Returns true if there are on document in database based on the provided arguments.
75 76 77 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 75 def exists? with_default_scope.exists? end |
#find(*args) ⇒ Document, ...
Find a Document
in several different ways.
If a String
is provided, it will be assumed that it is a representation of a Mongo::ObjectID and will attempt to find a single Document
based on that id. If a Symbol
and Hash
is provided then it will attempt to find either a single Document
or multiples based on the conditions provided and the first parameter.
93 94 95 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 93 def find(*args) with_default_scope.find(*args) end |
#find_by(attrs = {}) {|result| ... } ⇒ Document?
Find the first Document
given the conditions. If a matching Document is not found and Mongoid.raise_not_found_error is true it raises Mongoid::Errors::DocumentNotFound, return null nil elsewise.
and Mongoid.raise_not_found_error is true.
113 114 115 116 117 118 119 120 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 113 def find_by(attrs = {}) result = where(attrs).find_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_by!(attrs = {}) {|result| ... } ⇒ Document
Find the first Document
given the conditions, or raises Mongoid::Errors::DocumentNotFound
134 135 136 137 138 139 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 134 def find_by!(attrs = {}) result = where(attrs).find_first raise(Errors::DocumentNotFound.new(self, attrs)) unless result yield(result) if result && block_given? result end |
#first ⇒ Document Also known as: one
Find the first Document
given the conditions.
147 148 149 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 147 def first with_default_scope.first end |
#last ⇒ Document
Find the last Document
given the conditions.
158 159 160 |
# File 'build/mongoid-6.1/lib/mongoid/findable.rb', line 158 def last with_default_scope.last end |