Avoid reify error on paper_trail

2025-08-02 ruby rails

When I tryed to use paper_trail gem in a new Ruby on Rails project, I have encoutered the error like the below:

Psych::DisallowedClass: Tried to load unspecified class: Date

After discussing in the issue, found 3 ways to solve the problem.

https://github.com/paper-trail-gem/paper_trail/issues/1526

1 Use JSON as the serializer(recommened for new project).

PaperTrail.serializer = PaperTrail::Serializers::JSON

2 Add permmietd classes for ActiveRecord column.

ActiveRecord.yaml_column_permitted_classes += [Date, Time, String]

3 Use unsafe_load.

ActiveRecord.use_yaml_unsafe_load = true

(Added on 26.01.27) 4 Create custom YAML serializer with permitted classes. config/initializers/paper_trail.rb

PaperTrail.config.serializer = PaperTrail::Serializers::YAML

module PaperTrail
  module Serializers
    module YAML
      extend self

      def load(string)
        ::YAML.safe_load(string, permitted_classes: [
          Time,
          Date,
          DateTime,
          ActiveSupport::TimeWithZone,
          ActiveSupport::TimeZone,
          Symbol,
          BigDecimal
        ], aliases: true)
      end

      def dump(object)
        ::YAML.dump(object)
      end
    end
  end
end