Hey!
I'm currently working with the latest version of Active Model Serializers and was wondering how I could go about specifying different includes for Polymorphic relationships.
For example:
class FeedSerializer < ActiveModel::Serializers
has_many :stories, polymorphic: true
end
:stories could be a "Post" type or a "Photo" type for example.
My include is: 'author,comments,tags' but I would only like to select comments if it's a "Post" type. How do I include specific fields for different types on a polymorphic association?
I was wondering how I could go about doing this without patching serializable_hash. I have looked through the issues and documentation and have only found if_sideloaded but I don't quite understand if that could be applicable in this case.
Closing due to insufficient information to easily help.
Please review the requested information in our issue template and reopen with the additional information, or any questions and comments you may have.
Thanks for your help, and thanks for making this issue.
This isn't a bug report :( this is a request for more information regarding an incompatibility in the design of include for polymorphic associations.
Polymorphic associations should have unique association selections because requiring the same include across associations in a polymorphic array leads to unused data selection.
{ items: [
{ type: "Post", id: 1, comments: [...]},
{ type: "Photo", id: 1, comments: [...]}
}
{ items: [
{ type: "Post", id: 1, comments: [...]},
{ type: "Photo", id: 1, tags: [...]}
}
0.10.3
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
Ubuntu 14.04 LTS (GNU/Linux 3.13.0-24-generic x86_64)
Rails
N/A
N/A
@bf4 do I have to create a new issue for this? The reason I did not use the template initially is because this is not a bug report.
@colepatrickturner
My include is: 'author,comments,tags' but I would only like to select comments if it's a "Post" type
perhaps I misunderstood your issue and you are asking not about polymorphic, but how to set the JSON:API fields and include directives work to specify something like include=stories,post.comments,photo.tags (off the top of my head, probably syntax is off a bit). It's possible this is buggy in AMS. Not sure at moment.
Thank you for your time and response! Actually it is related to polymorphic types. I'm not certain that it's only the JSON:API but we are using include via the Attributes adapter.
Are you suggesting that it's currently possible to do as I've described above by using type (where type is "Photo" or "Post") and include associations as such:
include=stories,post.comments,photo.tags
If indeed, that would require a change in API schema such as:
{ stories: [
{ type: "Photo", photo: { ... } },
{ type: "Post", post: { ... } }
}
The example above is not truly polymorphic. I am looking for a solution that retains the polymorphic schema of stories while using include to select associations relevant to each type.
I would imagine it would look something like this:
include=stories, stories{Post}.comments, stories{Photo}.tags
Where "stories" is still polymorphic and the type is resolved syntactically. Am I on the wrong track with what I've observed above @bf4 ?
Did you try include=stories,post.comments,photo.tags? cc @beauby you are my include directive guru
I have tried that, it does not work with the expected/desired output. It would only work if post and photo were object/hash keys and wouldn't be polymorphic.
Please consider re-opening this issue as I think it's pertinent to making include and polymorphic associations cohesive.
@colepatrickturner The include parameter format is specified by the JSON API spec. It acts only on the "full" relationships.
In your case I would suggest either:
include the relationship and simply provide an URL to the related collection, and later query it with some filtering (e.g. /feeds/3/stories?filter[type]=post).Also, feel free to make your case for extending the include parameter syntax to support your use-case within the spec.
Sorry I am having trouble following. The example I have given is a rough analogue to what I am trying to do with the Attributes adapter.
Regarding your suggestions:
If include must adhere to JSON API spec, then the solution I am looking for lies within the Active Model Serializers API. I believe in order to preserve the schema and polymorphic associations, I must have to override associations in my serializer.
Thanks again for your time!
@coleturner I'm in the same situation here. Did you find some way to specify include in polymorphic?
Just another similar example. But we have a votable object that can be assigned to a Question as well as a Solution. A Question has many Solutions and both of them have votable objects. AMS seems to only allow the include of one or the other via include: 'votes' or include: solutions.votes, but you can't do include: 'votes, solutions, solutions.votes'