Method: Mongoid::Scopable::ClassMethods#scopes

Defined in:
lib/mongoid/scopable.rb

#scopesHash

Returns a hash of all the scopes defined for this class, including scopes defined on ancestor classes.

Examples:

Get the defined scopes for a class

class Band
  include Mongoid::Document
  field :active, type: Boolean

  scope :active, -> { where(active: true) }
end
Band.scopes

Returns:

  • (Hash)

    The scopes defined for this class

Since:

  • 3.1.4



56
57
58
59
60
61
62
63
64
# File 'lib/mongoid/scopable.rb', line 56

def scopes
  defined_scopes = {}
  ancestors.reverse.each do |klass|
    if klass.respond_to?(:_declared_scopes)
      defined_scopes.merge!(klass._declared_scopes)
    end
  end
  defined_scopes.freeze
end