11

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.

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
  • 3
    For me adding `config.active_record.yaml_column_permitted_classes = [Symbol, Date, Time, ActiveSupport::TimeWithZone, ActiveSupport::TimeZone]` in `config/application.rb ` solved the issue. Did you restart the server after changing this config file? – Ziv Galili Feb 27 '23 at 13:16

1 Answers1

6

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.

user16217248
  • 3,119
  • 19
  • 19
  • 37
erwin
  • 442
  • 6
  • 13
  • This didn't work for me. – huertanix Jul 30 '23 at 03:23
  • 1
    Sorry to hear that @huertanix What else did you try? Is there any other error information surrounding your error? Did the error change after updating your config? Did you restart the rails server? What are your ruby and rails versions? – erwin Jul 30 '23 at 06:30
  • I am getting this error for Date class Psych::DisallowedClass: Tried to load unspecified class: Date, I tried this but its not solving my issue. – vishesh268 Aug 04 '23 at 10:04
  • Using Rails 6.0.6.1 with Ruby 3.0.2. Tried adding this to the three environment configs too, but that did nothing. Also tried tenderlove's `config.active_record.use_yaml_unsafe_load` option, which is not a great idea security-wise, but that didn't work either. Naturally restarted my rails web server so many times. Even rebooting my whole machine just to completely rule out the possibility. Ended up changing my fixtures' Date fields to Strings by putting their values in quotes. It's inelegant, tedious, but that seems to be the vibe with this framework. – huertanix Aug 05 '23 at 00:00