Module: Mongoid::Finders
- Defined in:
- lib/mongoid/finders.rb
Overview
This module defines the finder methods that hang off the document at the class level.
Instance Method Summary collapse
-
#all(*args) ⇒ Criteria
Find all documents that match the given conditions.
-
#count(*args) ⇒ Integer
Returns a count of matching records in the database based on the provided arguments.
-
#empty? ⇒ true, false
Returns true if count is zero.
-
#exists?(*args) ⇒ Boolean
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_or_create_by(attrs = {}, &block) ⇒ Document
Find the first
Document
given the conditions, or creates a new document with the conditions that were supplied. -
#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. -
#first(*args) ⇒ Document
Find the first
Document
given the conditions. -
#last(*args) ⇒ Document
Find the last
Document
given the conditions.
Instance Method Details
#all(*args) ⇒ Criteria
Find all documents that match the given conditions.
25 26 27 |
# File 'lib/mongoid/finders.rb', line 25 def all(*args) find(:all, *args) end |
#count(*args) ⇒ Integer
Returns a count of matching records in the database based on the provided arguments.
38 39 40 |
# File 'lib/mongoid/finders.rb', line 38 def count(*args) find(:all, *args).count end |
#empty? ⇒ true, false
Returns true if count is zero
48 49 50 |
# File 'lib/mongoid/finders.rb', line 48 def empty? count == 0 end |
#exists?(*args) ⇒ Boolean
Returns true if there are on document in database based on the provided arguments.
59 60 61 |
# File 'lib/mongoid/finders.rb', line 59 def exists?(*args) find(:all, *args).count > 0 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.
83 84 85 |
# File 'lib/mongoid/finders.rb', line 83 def find(*args) criteria.find(*args) 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.
96 97 98 |
# File 'lib/mongoid/finders.rb', line 96 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.
109 110 111 |
# File 'lib/mongoid/finders.rb', line 109 def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end |
#first(*args) ⇒ Document
Find the first Document
given the conditions.
121 122 123 |
# File 'lib/mongoid/finders.rb', line 121 def first(*args) find(:first, *args) end |
#last(*args) ⇒ Document
Find the last Document
given the conditions.
133 134 135 |
# File 'lib/mongoid/finders.rb', line 133 def last(*args) find(:last, *args) end |