mongodb/mongoid · error · ArgumentError

Method '# ' is not allowed in from_hash

Error message

Method '#{method}' is not allowed in from_hash

What it means

from_hash only translates a whitelist of criteria methods (where, order_by, limit, skip, etc.) from hash form. A key naming anything else — a typo or an unsupported method — is rejected with this ArgumentError.

Solutions

  1. Use only the supported criteria method names as keys in the hash
  2. Prefer building the criteria with the fluent API instead of the deprecated from_hash
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/criteria.rb:86 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/b0a48cf4bd3eb269. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/criteria.rb:86

      # can include a "klass" key that specifies the model class for the criteria.
      #
      # @example Convert the hash to a criteria.
      #   Criteria.from_hash({ klass: Band, where: { name: "Depeche Mode" })
      #
      # @deprecated This method is deprecated and will
      #  be removed in a future release.
      #
      # @param [ Hash ] hash The hash to convert.
      #
      # @return [ Criteria ] The criteria.
      #
      # @raise [ ArgumentError ] If a method is not allowed in from_hash.
      def from_hash(hash)
        criteria = Criteria.new(hash.delete(:klass) || hash.delete('klass'))
        hash.each_pair do |method, args|
          method_sym = method.to_sym
          unless ALLOWED_FROM_HASH_METHODS.include?(method_sym)
            raise ArgumentError, "Method '#{method}' is not allowed in from_hash"
          end

          criteria = criteria.public_send(method_sym, args)
        end
        criteria
      end
      Mongoid.deprecate(self, :from_hash)
    end

    # Static array used to check with method missing - we only need to ever
    # instantiate once.
    CHECK = []

    attr_accessor :embedded, :klass, :parent_document, :association

    # Returns true if the supplied +Enumerable+ or +Criteria+ is equal to the results
    # of this +Criteria+ or the criteria itself.
    #

View on GitHub (pinned to 0da0e23d71)