Netflix / fast_jsonapi

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.
Apache License 2.0
5.07k stars 425 forks source link

Support custom id methods for polymorphic relationships #441

Closed aub closed 4 years ago

aub commented 4 years ago

For polymorphic has_many relations, instead of getting the ids in a batch, the code loops over each object to get its type and id. In Relationship#id_hash_from_record it was hardcoded to get the id by calling record.id, which overrides the id_method_name options passed into the relationship.

I tried fixing this by only changing the code in Relationship to dynamically call the id_method_name on the object, but unfortunately on has_many relationships the default value for id_method_name would be passed as if the ids would be pulled in a batch (for example groupee_ids). That default works for regular has_manys where we get the ids all at once but fails for polymorphic ones where it loops over each object.

So, fixing the problem required passing the polymorphic information to compute_id_method_name, which now defaults to id rather than id_method_name_from_relationship in the case where the relationship is polymorphic.

aub commented 4 years ago

I also added a fix for the custom id method not carrying over through inheritance.