Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Tried to load unspecified class Psych::DisallowedClass of a model #388

Open
palhimalaya opened this issue May 29, 2024 · 6 comments
Assignees

Comments

@palhimalaya
Copy link
Contributor

Note: Quote is my model name.

After upgrading from Rails 5 to Rails 6 and from Ruby 2.3 to Ruby 3, I encountered an error. The error message indicates an issue with loading an unspecified class Quote. Below are the details of the error and the relevant part of the code:

pry(#<#<Class:0x000061b9016dd758>>)> activity.parameters
Psych::DisallowedClass: Tried to load unspecified class: Quote
from /home/cosine/.rbenv/versions/3.0.2/lib/ruby/3.0.0/psych/class_loader.rb:99:in `find'
[2] pry(#<#<Class:0x000061b9016dd758>>)> activity
=> #<PublicActivity::Activity:0x000061b901421898
 id: 272678,
 trackable_type: "Quote",
 trackable_id: 28253,
 owner_type: "User",
 owner_id: 48,
 key: "activity.quote.project_details_updated",
 parameters: #<PublicActivity::Activity:0x12fd4>
[3] pry(#<#<Class:0x000061b9016dd758>>)> 
@walterdavis
Copy link

walterdavis commented May 29, 2024 via email

@palhimalaya
Copy link
Contributor Author

palhimalaya commented May 29, 2024


@quote = Quote.find(self.id)
@quote.create_activity(
  key: 'activity.quote.project_details_updated',
  params: { 
    user_role: @current_user.roles.pluck(:name), 
    project_details: self.changed_attributes, 
    current_project_details: @quote 
  }, 
  owner: @current_user
)

As you can see, in the activity creation part, I am sending the actual ActiveRecord @quote in current_project_details.

I am trying to fix this issue by converting it to a hash. In my case, I am using OpenStruct like this:

@quote = Quote.find(self.id)
quote_struct = OpenStruct.new(@quote.attributes)
@quote.create_activity(
  key: 'activity.quote.project_details_updated',
  params: { 
    user_role: @current_user.roles.pluck(:name), 
    project_details: self.changed_attributes, 
    current_project_details: quote_struct 
  }, 
  owner: @current_user
)

I don't think this is the proper solution as it requires changing every old data entry.

Also i am not using psych gem.

Other than this, the version upgrade is going well with some minor syntax changes.


@walterdavis
Copy link

walterdavis commented May 29, 2024 via email

@palhimalaya
Copy link
Contributor Author

I will look into it.

@pokonski
Copy link
Member

pokonski commented May 31, 2024

Seems like you are serializing whole Active Record objects which I would definitely NOT do.

A bypass for this is allowing this class to be serialized, but why do you need an actual instance of the record and not just its id?

@palhimalaya
Copy link
Contributor Author

I am currently upgrading old code written by other developers in Ruby 2 and Rails 5 to Ruby 3 and Rails 6. As part of this upgrade, I have encountered legacy code that relies on serializing entire Active Record objects.

While it would be ideal to only serialize the IDs and fetch the records as needed, this approach doesn't fit seamlessly with the existing codebase. I did attempt to bypass the issue by allowing the class to be serialized, but this led to improper serialization and subsequent data issues. As a workaround, I am manipulating the old data to fit the current code by converting it into OpenStruct.

@ur5us ur5us self-assigned this Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants