{"record":{"id":"d1b4e5dbe2d855a1","repo":"mongodb/mongoid","slug":"attempted-to-persist-a-readonly-document-of-class-d1b4e5","errorCode":null,"errorMessage":"Attempted to persist a readonly document of class '%{klass}'.","messagePattern":"Attempted to persist a readonly document of class '%(.+?)'\\.","errorType":"exception","errorClass":"Mongoid::Errors::ReadonlyDocument","httpStatus":null,"severity":"error","filePath":"lib/mongoid/persistable/destroyable.rb","lineNumber":22,"sourceCode":"  module Persistable\n    # Defines behavior for persistence operations that destroy documents.\n    module Destroyable\n      extend ActiveSupport::Concern\n\n      # Remove the document from the database with callbacks.\n      #\n      # @example Destroy a document.\n      #   document.destroy\n      #\n      # @param [ Hash ] options The options.\n      # @option options [ true | false ] :persist Whether to persist\n      #   the delete action. Callbacks will still be run even if false.\n      # @option options [ true | false ] :suppress Whether to update\n      #   the parent document in-memory when deleting an embedded document.\n      #\n      # @return [ true | false ] True if successful, false if not.\n      def destroy(options = nil)\n        raise Errors::ReadonlyDocument.new(self.class) if readonly?\n\n        self.flagged_for_destroy = true\n        result = run_callbacks(:commit, skip_if: -> { in_transaction? }) do\n          run_callbacks(:destroy) do\n            if catch(:abort) { apply_destroy_dependencies! }\n              delete(options || {}).tap do |res|\n                Threaded.add_modified_document(_session, self) if res && in_transaction?\n              end\n            else\n              false\n            end\n          end\n        end\n        self.flagged_for_destroy = false\n        result\n      end\n\n      # Remove the document from the database with callbacks. Raises","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/persistable/destroyable.rb#L4-L40","documentation":"Error \"Attempted to persist a readonly document of class '%{klass}'.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/persistable/destroyable.rb:22 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0da0e23d71adfbad31128c18e5e2c8c301c6f7c4","analyzedAt":"2026-08-23T06:25:47.656Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}