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

Cannot create scope : , because of existing method . .

Error message

Cannot create scope :%{scope_name}, because of existing method %{model_name}.%{scope_name}.

What it means

Raised by check_scope_name when declaring a scope whose name collides with an existing scope or method and scope overwrite exceptions are enabled (Mongoid.scope_overwrite_exception = true). By default a warning is issued instead.

Solutions

  1. Rename the new scope to avoid the collision
  2. Remove the existing scope/method with the same name
  3. Set Mongoid.scope_overwrite_exception = false to only warn on overwrites
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/scopable.rb:244 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/bc284e0c3f10e124. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/scopable.rb:244

        Threaded.exit_without_default_scope(self)
      end

      private

      # Warns or raises exception if overriding another scope or method.
      #
      # @api private
      #
      # @example Warn or raise error if name exists.
      #   Model.check_scope_name("test")
      #
      # @param [ String | Symbol ] name The name of the scope.
      #
      # @raise [ Errors::ScopeOverwrite ] If the name exists and configured to
      #   raise the error.
      def check_scope_name(name)
        return unless _declared_scopes[name] || respond_to?(name, true)
        raise Errors::ScopeOverwrite.new(self.name, name) if Mongoid.scope_overwrite_exception

        Mongoid.logger.warn(
          "Creating scope :#{name} which conflicts with #{self.name}.#{name}. " +
          "Calls to `Mongoid::Criteria##{name}` will delegate to " +
          "`Mongoid::Criteria##{name}` for criteria with klass #{self.name} " +
          '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.

View on GitHub (pinned to 0da0e23d71)