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/deletable.rb:108

      # @param [ Hash ] options The delete options.
      #
      # @return [ true | false ] If the parent should be notified.
      def notifying_parent?(options = {})
        !options.delete(:suppress)
      end

      # Prepare the delete operation.
      #
      # @api private
      #
      # @example Prepare the delete operation.
      #   document.prepare_delete do
      #     collection.find(atomic_selector).remove
      #   end
      #
      # @return [ true ] If the object was deleted successfully.
      def prepare_delete
        raise Errors::ReadonlyDocument.new(self.class) if readonly?

        yield(self)
        freeze
        self.destroyed = true
      end

      module ClassMethods
        # Delete all documents given the supplied conditions. If no conditions
        # are passed, the entire collection will be dropped for performance
        # benefits. Does not fire any callbacks.
        #
        # @example Delete matching documents from the collection.
        #   Person.delete_all({ :title => "Sir" })
        #
        # @example Delete all documents from the collection.
        #   Person.delete_all
        #
        # @param [ Hash ] conditions Optional conditions to delete by.

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/persistable/deletable.rb:108 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/2ca8d9aeb74dabe9. Report an issue: GitHub.