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/destroyable.rb:22

  module Persistable
    # Defines behavior for persistence operations that destroy documents.
    module Destroyable
      extend ActiveSupport::Concern

      # Remove the document from the database with callbacks.
      #
      # @example Destroy a document.
      #   document.destroy
      #
      # @param [ Hash ] options The options.
      # @option options [ true | false ] :persist Whether to persist
      #   the delete action. Callbacks will still be run even if false.
      # @option options [ true | false ] :suppress Whether to update
      #   the parent document in-memory when deleting an embedded document.
      #
      # @return [ true | false ] True if successful, false if not.
      def destroy(options = nil)
        raise Errors::ReadonlyDocument.new(self.class) if readonly?

        self.flagged_for_destroy = true
        result = run_callbacks(:commit, skip_if: -> { in_transaction? }) do
          run_callbacks(:destroy) do
            if catch(:abort) { apply_destroy_dependencies! }
              delete(options || {}).tap do |res|
                Threaded.add_modified_document(_session, self) if res && in_transaction?
              end
            else
              false
            end
          end
        end
        self.flagged_for_destroy = false
        result
      end

      # Remove the document from the database with callbacks. Raises

View on GitHub (pinned to 0da0e23d71)

When it happens

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