fast_jsonapi
fast_jsonapi copied to clipboard
id_method_name not respected with polymorphic association
Version: 1.5
Without polymorphic association, I can do:
belongs_to(
:event,
id_method_name: :event_uuid,
)
It correctly uses event_uuid as the method to get the id of the association.
With polymorphic though:
belongs_to(
:resource,
id_method_name: :resource_uuid,
polymorphic: true
)
id_method_name has no effect, it always uses resource_id as the id. I think this is a bug?
Can verify, also think this is a bug.
If this is a dealbreaker for someone I think you can work around it like this:
- Use
record_type: { Image: :image, etc }, this allows to useid_method_nameand sets types correctly - Create a single serializer for the polymorphic association. E.g.
ResourceSerializer - In
ResourceSerializeruse conditional attributes based on type, like so:
attributes :widht, :height, if: proc { |record, _params|
record.class == Image
}
I'm not sure how/if relationships would work in this case.
This is still a bug and makes the use of polymorphic serialization difficult.