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/updatable.rb:97

      end

      # Prepare the update for execution. Validates and runs callbacks, etc.
      #
      # @api private
      #
      # @example Prepare for update.
      #   document.prepare_update do
      #     collection.update(atomic_selector)
      #   end
      #
      # @param [ Hash ] options The options.
      #
      # @option options [ true | false ] :touch Whether or not the updated_at
      #   attribute will be updated with the current time.
      #
      # @return [ true | false ] The result of the update.
      def prepare_update(options = {})
        raise Errors::ReadonlyDocument.new(self.class) if readonly? && !Mongoid.legacy_readonly

        enforce_immutability_of_id_field!
        ensure_client_compatibility!
        return false if performing_validations?(options) &&
                        invalid?(options[:context] || :update)

        process_flagged_destroys
        update_children = cascadable_children(:update)
        process_touch_option(options) do
          run_all_callbacks_for_update(update_children) do
            result = yield(self)
            self.previously_new_record = false
            post_process_persist(result, options)
            true
          end
        end
      end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/persistable/updatable.rb:97 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/21826f3999367a1a. Report an issue: GitHub.