mongodb/mongoid · error · Mongoid::Errors::InvalidScope

Defining a scope of value %{value} on %{klass} is not allowe

Error message

Defining a scope of value %{value} on %{klass} is not allowed.

What it means

Error "Defining a scope of value %{value} on %{klass} is not allowed." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/scopable.rb:268

          'and will ignore the declared scope.'
        )
      end

      # Checks if the intended scope is a valid object, either a criteria or
      # proc with a criteria.
      #
      # @api private
      #
      # @example Check if the scope is valid.
      #   Model.check_scope_validity({})
      #
      # @param [ Object ] value The intended scope.
      #
      # @raise [ Errors::InvalidScope ] If the scope is not a valid object.
      def check_scope_validity(value)
        return if value.respond_to?(:call)

        raise Errors::InvalidScope.new(self, value)
      end

      # Defines the actual class method that will execute the scope when
      # called.
      #
      # @api private
      #
      # @example Define the scope class method.
      #   Model.define_scope_method(:active)
      #
      # @param [ Symbol ] name The method/scope name.
      #
      # @return [ Method ] The defined method.
      def define_scope_method(name)
        singleton_class.class_eval do
          define_method(name) do |*args, **kwargs|
            scoping = _declared_scopes[name]
            scope = instance_exec(*args, **kwargs, &scoping[:scope])

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/scopable.rb:268 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/5b92025917a2290f. Report an issue: GitHub.