Module: Mongoid::Finders

Defined in:
lib/mongoid/finders.rb

Overview

:nodoc:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Will execute a Criteria based on the DynamicFinder that gets generated.

Options:

name: The finder method name args: The arguments to pass to the method.

Example:

Person.find_all_by_title_and_age("Sir", 30)



89
90
91
92
93
94
# File 'lib/mongoid/finders.rb', line 89

def method_missing(name, *args)
  dyna = DynamicFinder.new(name, *args)
  finder, conditions = dyna.finder, dyna.conditions
  results = find(finder, :conditions => conditions)
  results ? results : dyna.create(self)
end

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



11
12
13
# File 'lib/mongoid/finders.rb', line 11

def all(*args)
  find(*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" })



19
20
21
# File 'lib/mongoid/finders.rb', line 19

def count(*args)
  Criteria.translate(self, *args).count
end

#criteriaObject

Helper to initialize a new Criteria object for this class.

Example:

Person.criteria



28
29
30
# File 'lib/mongoid/finders.rb', line 28

def criteria
  Criteria.new(self)
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)



45
46
47
48
49
50
51
52
53
54
# File 'lib/mongoid/finders.rb', line 45

def find(*args)
  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

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



63
64
65
# File 'lib/mongoid/finders.rb', line 63

def first(*args)
  find(*args).one
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" })



74
75
76
# File 'lib/mongoid/finders.rb', line 74

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

#only(*args) ⇒ Object

Entry point for creating a new criteria from a Document. This will instantiate a new Criteria object with the supplied select criterion already added to it.

Options:

args: A list of field names to retrict the returned fields to.

Example:

Person.only(:field1, :field2, :field3)

Returns: Criteria



126
127
128
# File 'lib/mongoid/finders.rb', line 126

def only(*args)
  Criteria.new(self).only(*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.



109
110
111
# File 'lib/mongoid/finders.rb', line 109

def paginate(params = {})
  Criteria.translate(self, params).paginate
end

#where(selector = nil) ⇒ Object

Entry point for creating a new criteria from a Document. This will instantiate a new Criteria object with the supplied select criterion already added to it.

Options:

selector: A where criteria to initialize.

Example:

Person.where(:field1 => "Value")

Returns: Criteria



143
144
145
# File 'lib/mongoid/finders.rb', line 143

def where(selector = nil)
  Criteria.new(self).where(selector)
end