This module defines the finder methods that hang off the document at the class level.
Find all documents that match the given conditions.
@example Find all matching documents given conditions.
Person.all(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions with options.
@return [ Criteria ] The matching documents.
# File lib/mongoid/finders.rb, line 24 def all(*args) find(:all, *args) end
Returns a count of matching records in the database based on the provided arguments.
@example Get the count of matching documents.
Person.count(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions.
@return [ Integer ] The number of matching documents.
# File lib/mongoid/finders.rb, line 37 def count(*args) find(:all, *args).count end
Returns true if count is zero
@example Are there no saved documents for this model?
Person.empty?
@return [ true, false ] If the collection is empty.
# File lib/mongoid/finders.rb, line 47 def empty? count == 0 end
Returns true if there are on document in database based on the provided arguments.
@example Do any documents exist for the conditions?
Person.exists?(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions.
# File lib/mongoid/finders.rb, line 58 def exists?(*args) find(:all, *args).limit(1).count == 1 end
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 Find the first matching document.
Person.find(:first, :conditions => { :attribute => "value" })
@example Find all matching documents.
Person.find(:all, :conditions => { :attribute => "value" })
@example Find a single document by an id.
Person.find(BSON::ObjectId)
@param [ Array ] args An assortment of finder options.
@return [ Document, nil, Criteria ] A document or matching documents.
# File lib/mongoid/finders.rb, line 82 def find(*args) criteria.find(*args) end
Find the first Document given the conditions, or creates a new document with the conditions that were supplied.
@example Find or create the document.
Person.find_or_create_by(:attribute => "value")
@param [ Hash ] attrs The attributes to check.
@return [ Document ] A matching or newly created document.
# File lib/mongoid/finders.rb, line 95 def find_or_create_by(attrs = {}, &block) find_or(:create, attrs, &block) end
Find the first Document given the conditions, or initializes a new document with the conditions that were supplied.
@example Find or initialize the document.
Person.find_or_initialize_by(:attribute => "value")
@param [ Hash ] attrs The attributes to check.
@return [ Document ] A matching or newly initialized document.
# File lib/mongoid/finders.rb, line 108 def find_or_initialize_by(attrs = {}, &block) find_or(:new, attrs, &block) end
Find the first Document given the conditions.
@example Find the first document.
Person.first(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions with options.
@return [ Document ] The first matching document.
# File lib/mongoid/finders.rb, line 120 def first(*args) find(:first, *args) end
Find the last Document given the conditions.
@example Find the last document.
Person.last(:conditions => { :attribute => "value" })
@param [ Array ] args The conditions with options.
@return [ Document ] The last matching document.
# File lib/mongoid/finders.rb, line 132 def last(*args) find(:last, *args) end
Find the first object or create/initialize it.
@example Find or perform an action.
Person.find_or(:create, :name => "Dev")
@param [ Symbol ] method The method to invoke. @param [ Hash ] attrs The attributes to query or set.
@return [ Document ] The first or new document.
# File lib/mongoid/finders.rb, line 147 def find_or(method, attrs = {}, &block) first(:conditions => attrs) || send(method, attrs, &block) end
Generated with the Darkfish Rdoc Generator 2.