fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

id_method_name not respected with polymorphic association

Open doits opened this issue 6 years ago • 3 comments

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?

doits avatar Jan 22 '19 18:01 doits

Can verify, also think this is a bug.

thisismydesign avatar Mar 25 '19 14:03 thisismydesign

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 use id_method_name and sets types correctly
  • Create a single serializer for the polymorphic association. E.g. ResourceSerializer
  • In ResourceSerializer use 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.

thisismydesign avatar Mar 25 '19 16:03 thisismydesign

This is still a bug and makes the use of polymorphic serialization difficult.

maxKimoby avatar Apr 23 '20 16:04 maxKimoby