resource links on a polymorphic belongs_to association#81
resource links on a polymorphic belongs_to association#81camallen wants to merge 1 commit intoRestPack:masterfrom
Conversation
|
There isn't anything in the JSON API format about polymorphic relationships, and I think that's because it's outside the scope of the format. There are ways of handling this in the response structure itself though. Instead of referring to the polymorphic name you're likely using in your Ruby code, you should instead refer to the actual class it is cast to. That also means you'll need to use the legitimate class names when defining "links" or "linked" relationships. For example, instead of this: ...you'd instead need to force it to output something like this: |
|
It seems the JSON API spec has changed recently and they now allow the link to represented as a resource object with a type param, e.g: {
"id": "1",
"title": "Rails is Omakase",
"links": {
"author": {
"href": "http://example.com/people/17",
"id": "17",
"type": "people"
}
}
}The spec now states NOTE: In case of conflict, an individual resource object's links object will take precedence over a top-level links object. This would allow the links resource representation to be authoritative. I think Restpack Serializer could now reflect upon the association and if it's polymorphic it could output a resource object representing the association. I'll try and implement some code and update some specs to achieve this. |
|
closing in favour of #122 |
I wanted to start a discussion around how to handle a polymorphic belongs_to relation on a resource.
Just returning the FK id doesn't contain the polymorphic context of the linked resource, resulting in an ambiguous link. I've implemented a simple fix that fits my needs as my polymorphic association target overrides the to_param method however if the association target doesn't then this problem still remains.
Perhaps there is a better way of embedding the association type in the links hash? Thoughts?