Module: Mongoid::DefaultScope::ClassMethods

Defined in:
lib/mongoid/default_scope.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#default_scope(conditions = {}) ⇒ Object

Creates a default_scope for the Document, similar to ActiveRecord’s default_scope. DefaultScopes are proxied Criteria objects that are applied by default to all queries for the class.

Examples:

Create a default scope.


class Person
  include Mongoid::Document
  field :active, :type => Boolean
  field :count, :type => Integer

  default_scope :where => { :active => true }
end

Parameters:

  • conditions (Hash) (defaults to: {})

    The conditions to create with.

Since:

  • 2.0.0.rc.1



31
32
33
# File 'lib/mongoid/default_scope.rb', line 31

def default_scope(conditions = {})
  self.default_scoping = Scope.new(conditions).conditions.scoped
end