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.
24 25 26 |
# File 'lib/mongoid/finders.rb', line 24 def all(*args) find(:all, *args) end |
#count(*args) ⇒ Integer
Returns a count of matching records in the database based on the provided arguments.
37 38 39 |
# File 'lib/mongoid/finders.rb', line 37 def count(*args) find(:all, *args).count end |
#empty? ⇒ true, false
Returns true if count is zero
47 48 49 |
# File 'lib/mongoid/finders.rb', line 47 def empty? count == 0 end |
#exists?(*args) ⇒ Boolean
Returns true if there are on document in database based on the provided arguments.
58 59 60 |
# File 'lib/mongoid/finders.rb', line 58 def exists?(*args) find(:all, *args).limit(1).count == 1 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.
82 83 84 |
# File 'lib/mongoid/finders.rb', line 82 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.
95 96 97 |
# File 'lib/mongoid/finders.rb', line 95 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.
108 109 110 |
# File 'lib/mongoid/finders.rb', line 108 def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end |
#first(*args) ⇒ Document
Find the first Document
given the conditions.
120 121 122 |
# File 'lib/mongoid/finders.rb', line 120 def first(*args) find(:first, *args) end |
#last(*args) ⇒ Document
Find the last Document
given the conditions.
132 133 134 |
# File 'lib/mongoid/finders.rb', line 132 def last(*args) find(:last, *args) end |