active_model_serializers: JSON adapter does not handle nested associations
It only supports the associations in the model being serialized. For instance if you serialize Blog, it will serialize the posts, but not the comments of each post.
This line https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/adapter/json.rb#L14 should probably be changed to get a serializable_hash
instead of just attributes.
This was discovered while trying existing 0.9.x code on the master branch. Is this something that should be supported? If so, I can submit a pull request for it.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 38 (22 by maintainers)
Well, firstly, it’s on the
JSON API
section, but it works for theJSON
adapter 😃Is there anything specific to to get this working as mentioned by @richmolj? It’s not working for me using the latest on master and with the json-api adapter.
Do you need to define the nested relationship in the serializer?
So #1158 and #1127 solve this problem. If you want to serialize all defined relationships (which might be dangerous if you have cycles in your data graph), you would just provide the
include: '**'
option to the adapter. You could also manually specify the include tree, or just specify a level of nesting (likeinclude: '*.*.*'
for 3 levels).Closing this for now, feel free to reopen if needed.
@frahugo the work around would be something like bellow:
Instead of use
has_many :comments
, you can add it as an attribute and define a method that will overlap it’s value, inside this method you can mount any kind of object you want. In the example bellow I’m returning also the user that is related with the comments. do this solve your problem?