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

ActiveJob::SerializationError: #184

Open
Hammam94 opened this issue Sep 15, 2024 · 4 comments
Open

ActiveJob::SerializationError: #184

Hammam94 opened this issue Sep 15, 2024 · 4 comments

Comments

@Hammam94
Copy link

i am using paper_trail-background gem to version the records in the background your gem is not compatible with activejob as it raises the following issue
ActiveJob::SerializationError: Unsupported argument type: Inventory::JsonbModels::Telephone
Here is the store model class

module Inventory
  module JsonbModels
    class Telephone
      include StoreModel::Model

      enum :type, {
        fax: 'fax',
        phone: 'phone'
      }, default: :phone

      attribute :number, :string
    end
  end
end

any solution for that error

@DmitryTsepelev
Copy link
Owner

Hey! I've never used this combination of gems before, you'll have to figure out the solution and let me know 🙂 Probably you just need to define a serialization method (https://api.rubyonrails.org/classes/ActiveJob/SerializationError.html)

@Hammam94
Copy link
Author

i solved this by adding this custom serializer in the initializers folder please check it and tell me what do you think

module ActiveJob
  module Serializers
    class StoreModelSerializer < ActiveJob::Serializers::ObjectSerializer
      def serialize?(argument)
        argument.is_a?(StoreModel::Model)
      end

      def serialize(store_model)
        super(
          'attributes' => store_model.attributes,
          'class' => store_model.class.name
        )
      end

      def deserialize(hash)
        klass = hash['class'].constantize
        klass.new(hash['attributes'])
      end
    end
  end
end

ActiveJob::Serializers.add_serializers(ActiveJob::Serializers::StoreModelSerializer)

@DmitryTsepelev
Copy link
Owner

Looks nice! Do you think we should have it in the gem or docs at least?

@Hammam94
Copy link
Author

i think its better to be in the gem

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

2 participants