Search code examples
ruby-on-railsyamlactivesupport

Tried to load unspecified class: ActiveSupport::TimeWithZone - Psych::DisallowedClass


Does anyone know how to fix this exception? Not sure if it's related but it is falling on #discard method calling from discard gem.

if object.discard
   # Psych::DisallowedClass:
   #   Tried to load unspecified class: ActiveSupport::TimeWithZone

I've tried each of the following config settings added to config/application.rb but the issue still persists(no spring running, no preloading)

# config.active_record.yaml_column_permitted_classes = [Symbol, Hash, Array, ActiveSupport::HashWithIndifferentAccess, ActiveSupport::TimeWithZone, Time]
# config.active_record.use_yaml_unsafe_load
# config.active_support.use_yaml_unsafe_load

Ruby version: 3.1.2 | Rails version: 6.1.7

Related question:

Upgrading to Ruby 3.1 causes Psych::DisallowedClass exception when using YAML.load_file

As a temporary workaround, I've rolled back to 6.1.6 Rails version but I'm looking for a proper solution to this issue.


Solution

  • Add the following to the file config/application.rb.

    config.active_record.yaml_column_permitted_classes = [Symbol, Date, Time, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone]
    

    It should solve the issue. Then restart the server and reload! the Rails console to ensure it works.

    Credit to a comment above for providing this solution.