{"record":{"id":"efdbe9966363f004","repo":"mongodb/mongoid","slug":"attempted-to-persist-a-readonly-document-of-class-efdbe9","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/upsertable.rb","lineNumber":69,"sourceCode":"\n      private\n\n      # Prepare the upsert for execution.\n      #\n      # @api private\n      #\n      # @example Prepare the upsert\n      #   document.prepare_upsert do\n      #     collection.find(selector).update(as_document)\n      #   end\n      #\n      # @param [ Hash ] options The options hash.\n      #\n      # @option options [ true | false ] :validate Whether or not to validate.\n      #\n      # @return [ true | false ] If the operation succeeded.\n      def prepare_upsert(options = {})\n        raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly\n        return false if performing_validations?(options) && invalid?(:upsert)\n\n        result = run_callbacks(:upsert) do\n          yield(self)\n          true\n        end\n        self.new_record = false\n        post_process_persist(result, options) and result\n      end\n    end\n  end\nend\n","sourceCodeStart":51,"sourceCodeEnd":82,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/persistable/upsertable.rb#L51-L82","documentation":"Error \"Attempted to persist a readonly document of class '%{klass}'.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/persistable/upsertable.rb:69 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"}