'Specifying custom serializer for a relationship has_one json-api-rb

I have serializers:

class SerializableContact < JSONAPI::Serializable::Resource
  type :contacts

  has_one :email_event, class: 'SerializableEmailEvent' # class is not working
  has_many :geofence_notifications

  attributes :email,
             :order_id,
             :frequent_order_id
end

class SerializableEmailEvent < JSONAPI::Serializable::Resource
  ...
end

Models:

class Contact < ApplicationRecord
  has_one :email_event, -> { email }, class_name: 'Event'
end

class Event < ApplicationRecord
end

I am trying to make SerializableContact to use SerializableEmailEvent as the serializer for email_event relationship. But I cant figure out how to do that. It always error out with:

JsonapiCompliable::Errors::MissingSerializer - Could not find serializer for class 'Event'!

Looked for 'SerializableEvent' but doesn't appear to exist.

Use a custom Inferrer if you'd like different lookup logic.

I am not sure how to use a custom inferrer for the different lookup logic



Solution 1:[1]

Maybe I am a bit late responding to your issue, but here you have how to do it:

has_one :email_event do
  linkage(always: true) do
    { type: "email_event", id: @object.email_event_id } if @object.email_event_id
  end
end

They type has to match with the type on your serilizable resource class. So I was expecting it to be:

class SerializableEmailEvent < JSONAPI::Serializable::Resource
  type "email_event"
  ...
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fran Martinez