Module: Mongoid::Finders
- Defined in:
- lib/mongoid/finders.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#all(*args) ⇒ Object
Find
Documents
given the conditions. -
#count(*args) ⇒ Object
Returns a count of matching records in the database based on the provided arguments.
-
#criteria ⇒ Object
Helper to initialize a new
Criteria
object for this class. -
#exists?(*args) ⇒ Boolean
Returns true if there are on document in database based on the provided arguments.
-
#find(*args) ⇒ Object
Find a
Document
in several different ways. -
#find_or_create_by(attrs = {}) ⇒ Object
Find the first
Document
given the conditions, or creates a new document with the conditions that were supplied. -
#find_or_initialize_by(attrs = {}) ⇒ Object
Find the first
Document
given the conditions, or instantiates a new document with the conditions that were supplied. -
#first(*args) ⇒ Object
Find the first
Document
given the conditions. -
#last(*args) ⇒ Object
Find the last
Document
given the conditions. -
#paginate(params = {}) ⇒ Object
Find all documents in paginated fashion given the supplied arguments.
Instance Method Details
#all(*args) ⇒ Object
Find Documents
given the conditions.
Options:
args: A Hash
with a conditions key and other options
Person.all(:conditions => { :attribute => "value" })
21 22 23 |
# File 'lib/mongoid/finders.rb', line 21 def all(*args) find(:all, *args) end |
#count(*args) ⇒ Object
Returns a count of matching records in the database based on the provided arguments.
Person.count(:first, :conditions => { :attribute => "value" })
29 30 31 |
# File 'lib/mongoid/finders.rb', line 29 def count(*args) Criteria.translate(self, *args).count end |
#criteria ⇒ Object
Helper to initialize a new Criteria
object for this class.
Example:
Person.criteria
46 47 48 |
# File 'lib/mongoid/finders.rb', line 46 def criteria Criteria.new(self) end |
#exists?(*args) ⇒ Boolean
Returns true if there are on document in database based on the provided arguments.
Person.exists?(:conditions => { :attribute => "value" })
37 38 39 |
# File 'lib/mongoid/finders.rb', line 37 def exists?(*args) Criteria.translate(self, *args).limit(1).count == 1 end |
#find(*args) ⇒ Object
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.
Person.find(:first, :conditions => { :attribute => "value" })
Person.find(:all, :conditions => { :attribute => "value" })
Person.find(Mongo::ObjectId.new.to_s)
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mongoid/finders.rb', line 63 def find(*args) raise Errors::InvalidOptions.new("Calling Document#find with nil is invalid") if args[0].nil? type = args.delete_at(0) if args[0].is_a?(Symbol) criteria = Criteria.translate(self, *args) case type when :first then return criteria.one when :last then return criteria.last else return criteria end end |
#find_or_create_by(attrs = {}) ⇒ Object
Find the first Document
given the conditions, or creates a new document with the conditions that were supplied
Options:
args: A Hash
of attributes
Person.find_or_create_by(:attribute => "value")
83 84 85 |
# File 'lib/mongoid/finders.rb', line 83 def find_or_create_by(attrs = {}) find_or(:create, attrs) end |
#find_or_initialize_by(attrs = {}) ⇒ Object
Find the first Document
given the conditions, or instantiates a new document with the conditions that were supplied
Options:
args: A Hash
of attributes
Person.find_or_initialize_by(:attribute => "value")
95 96 97 |
# File 'lib/mongoid/finders.rb', line 95 def find_or_initialize_by(attrs = {}) find_or(:new, attrs) end |
#first(*args) ⇒ Object
Find the first Document
given the conditions.
Options:
args: A Hash
with a conditions key and other options
Person.first(:conditions => { :attribute => "value" })
106 107 108 |
# File 'lib/mongoid/finders.rb', line 106 def first(*args) find(:first, *args) end |
#last(*args) ⇒ Object
Find the last Document
given the conditions.
Options:
args: A Hash
with a conditions key and other options
Person.last(:conditions => { :attribute => "value" })
117 118 119 |
# File 'lib/mongoid/finders.rb', line 117 def last(*args) find(:last, *args) end |
#paginate(params = {}) ⇒ Object
Find all documents in paginated fashion given the supplied arguments. If no parameters are passed just default to offset 0 and limit 20.
Options:
params: A Hash
of params to pass to the Criteria API.
Example:
Person.paginate(:conditions => { :field => "Test" }, :page => 1, :per_page => 20)
Returns paginated array of docs.
134 135 136 |
# File 'lib/mongoid/finders.rb', line 134 def paginate(params = {}) Criteria.translate(self, params).paginate end |