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/creatable.rb:121
remember_storage_options!
flag_descendants_persisted
true
end
# Prepare the insert for execution. Validates and runs callbacks, etc.
#
# @api private
#
# @example Prepare for insertion.
# document.prepare_insert do
# collection.insert(as_document)
# end
#
# @param [ Hash ] options The options.
#
# @return [ Document ] The document.
def prepare_insert(options = {})
raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly
return self if performing_validations?(options) &&
invalid?(options[:context] || :create)
ensure_client_compatibility!
run_callbacks(:commit, with_children: true, skip_if: -> { in_transaction? }) do
run_callbacks(:save, with_children: false) do
run_callbacks(:create, with_children: false) do
run_callbacks(:persist_parent, with_children: false) do
_mongoid_run_child_callbacks(:save) do
_mongoid_run_child_callbacks(:create) do
result = yield(self)
if !result.is_a?(Document) || result.errors.empty?
post_process_insert
post_process_persist(result, options)
end
end
end
endView on GitHub (pinned to 0da0e23d71)
When it happens
Trigger: Thrown at lib/mongoid/persistable/creatable.rb:121 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/eeb3e30ff593241e.
Report an issue: GitHub.