{"record":{"id":"eeb3e30ff593241e","repo":"mongodb/mongoid","slug":"attempted-to-persist-a-readonly-document-of-class-eeb3e3","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/creatable.rb","lineNumber":121,"sourceCode":"        remember_storage_options!\n        flag_descendants_persisted\n        true\n      end\n\n      # Prepare the insert for execution. Validates and runs callbacks, etc.\n      #\n      # @api private\n      #\n      # @example Prepare for insertion.\n      #   document.prepare_insert do\n      #     collection.insert(as_document)\n      #   end\n      #\n      # @param [ Hash ] options The options.\n      #\n      # @return [ Document ] The document.\n      def prepare_insert(options = {})\n        raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly\n        return self if performing_validations?(options) &&\n                       invalid?(options[:context] || :create)\n\n        ensure_client_compatibility!\n        run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do\n          run_callbacks(:save, with_children: false) do\n            run_callbacks(:create, with_children: false) do\n              run_callbacks(:persist_parent, with_children: false) do\n                _mongoid_run_child_callbacks(:save) do\n                  _mongoid_run_child_callbacks(:create) do\n                    result = yield(self)\n                    if !result.is_a?(Document) || result.errors.empty?\n                      post_process_insert\n                      post_process_persist(result, options)\n                    end\n                  end\n                end\n              end","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/mongodb/mongoid/blob/0da0e23d71adfbad31128c18e5e2c8c301c6f7c4/lib/mongoid/persistable/creatable.rb#L103-L139","documentation":"Error \"Attempted to persist a readonly document of class '%{klass}'.\" thrown in mongodb/mongoid.","triggerScenarios":"Thrown at lib/mongoid/persistable/creatable.rb:121 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"}