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

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" })



25
26
27
# File 'lib/mongoid/finders.rb', line 25

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(:conditions => { :attribute => "value" })



33
34
35
# File 'lib/mongoid/finders.rb', line 33

def count(*args)
  find(:all, *args).count
end

#empty?Boolean

Returns true if count is zero

Returns:



38
39
40
# File 'lib/mongoid/finders.rb', line 38

def empty?
  count == 0
end

#exists?(*args) ⇒ Boolean

Returns true if there are on document in database based on the provided arguments.

Person.exists?(:conditions => { :attribute => "value" })

Returns:



46
47
48
# File 'lib/mongoid/finders.rb', line 46

def exists?(*args)
   find(:all, *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.

Example:

Person.find(:first, :conditions => { :attribute => "value" }) Person.find(:all, :conditions => { :attribute => "value" }) Person.find(BSON::ObjectId)

Options:

args: An assortment of finder options.

Returns:

A document or criteria.



71
72
73
# File 'lib/mongoid/finders.rb', line 71

def find(*args)
  criteria.find(*args)
end

#find_or_create_by(attrs = {}, &block) ⇒ 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 = {}, &block)
  find_or(:create, attrs, &block)
end

#find_or_initialize_by(attrs = {}, &block) ⇒ 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 = {}, &block)
  find_or(:new, attrs, &block)
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