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

Invalid persistence option

Error message

Invalid persistence option :%{invalid}.

What it means

Raised by the private PersistenceContext#set_options! while building a persistence context when a key in the provided options hash is not among the valid options (client, database, collection, collection_options, read, write, etc.). This generic guard rejects misspelled or unsupported persistence options such as with(store: ...) style overrides; the faulting input is the invalid option key.

Solutions

  1. Use only valid persistence options: :client, :collection, :database, and related recognized keys
  2. Check for typos in option names in the store_in / with options hash
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at lib/mongoid/persistence_context.rb:190 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/0e8b70d20d580295. Report an issue: GitHub.

Appendix: source

Thrown at lib/mongoid/persistence_context.rb:190

    end

    # The subset of provided options that may be used as storage
    # options.
    #
    # @return [ Hash | nil ] the requested storage options, or nil if
    #   none were specified.
    #
    # @api private
    def requested_storage_options
      slice = @options.slice(*Mongoid::Clients::Validators::Storage::VALID_OPTIONS)
      slice.any? ? slice : nil
    end

    private

    def set_options!(opts)
      @options ||= opts.each.reduce({}) do |_options, (key, value)|
        raise Errors::InvalidPersistenceOption.new(key.to_sym, VALID_OPTIONS) unless VALID_OPTIONS.include?(key.to_sym)

        value ? _options.merge!(key => value) : _options
      end
    end

    def __evaluate__(name)
      return nil unless name

      name.respond_to?(:call) ? name.call.to_sym : name.to_sym
    end

    def client_options
      @client_options ||= begin
        opts = options.select do |k, _v|
          Mongo::Client::VALID_OPTIONS.include?(k.to_sym)
        end
        opts[:read] = { mode: opts[:read] } if opts[:read].is_a?(Symbol)
        opts

View on GitHub (pinned to 0da0e23d71)