Module: Mongoid::Finders
- Extended by:
- Origin::Forwardable
- 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
-
#count ⇒ Integer
Returns a count of records in the database.
-
#empty? ⇒ true, false
Returns true if count is zero.
-
#exists? ⇒ 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_by(attrs = {}) {|result| ... } ⇒ Document
Find the first
Document
given the conditions, or raises Mongoid::Errors::DocumentNotFound. -
#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 ⇒ Document
Find the first
Document
given the conditions. -
#last ⇒ Document
Find the last
Document
given the conditions.
Instance Method Details
#count ⇒ Integer
Returns a count of records in the database. If you want to specify conditions use where.
21 22 23 |
# File 'lib/mongoid/finders.rb', line 21 def count with_default_scope.count end |
#empty? ⇒ true, false
Returns true if count is zero
31 32 33 |
# File 'lib/mongoid/finders.rb', line 31 def empty? count == 0 end |
#exists? ⇒ Boolean
Returns true if there are on document in database based on the provided arguments.
42 43 44 |
# File 'lib/mongoid/finders.rb', line 42 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.
60 61 62 |
# File 'lib/mongoid/finders.rb', line 60 def find(*args) with_default_scope.find(*args) end |
#find_by(attrs = {}) {|result| ... } ⇒ Document
Find the first Document
given the conditions, or raises Mongoid::Errors::DocumentNotFound
103 104 105 106 107 108 109 110 |
# File 'lib/mongoid/finders.rb', line 103 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_or_create_by(attrs = {}, &block) ⇒ Document
Find the first Document
given the conditions, or creates a new document with the conditions that were supplied.
73 74 75 |
# File 'lib/mongoid/finders.rb', line 73 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.
86 87 88 |
# File 'lib/mongoid/finders.rb', line 86 def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end |
#first ⇒ Document
Find the first Document
given the conditions.
118 119 120 |
# File 'lib/mongoid/finders.rb', line 118 def first with_default_scope.first end |
#last ⇒ Document
Find the last Document
given the conditions.
128 129 130 |
# File 'lib/mongoid/finders.rb', line 128 def last with_default_scope.last end |