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

Attempted to persist a readonly document of class '%{klass}'

Error message

Attempted to persist a readonly document of class '%{klass}'.

What it means

Error "Attempted to persist a readonly document of class '%{klass}'." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/persistable.rb:191

      post_persist unless result == false
      errors.clear unless performing_validations?(options)
      Threaded.add_modified_document(_session, self) if in_transaction?
      true
    end

    # Prepare an atomic persistence operation. Yields an empty hash to be sent
    # to the update.
    #
    # @api private
    #
    # @example Prepare the atomic operation.
    #   document.prepare_atomic_operation do |coll, selector, opts|
    #     ...
    #   end
    #
    # @return [ Object ] The result of the operation.
    def prepare_atomic_operation
      raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly

      operations = yield({})
      persist_or_delay_atomic_operation(operations)
      self
    end

    # Process the atomic operations - this handles the common behavior of
    # iterating through each op, getting the aliased field name, and removing
    # appropriate dirty changes.
    #
    # @api private
    #
    # @example Process the atomic operations.
    #   document.process_atomic_operations(pulls) do |field, value|
    #     ...
    #   end
    #
    # @param [ Hash ] operations The atomic operations.

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/persistable.rb:191 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/db0a6a958e9d6d98. Report an issue: GitHub.