ruby on rails - AMS: How to use collection serializer? -
i mean how should serializer implemented work collection?
from active model serializers documentation:
render json: @posts, serializer: paginatedserializer, each_serializer: postpreviewserializer
how should paginatedserializer
implemented?
i did this:
@posts = post.all render json: @posts
and worked ok , did not required nor special collection serializer, nor each_serializer.
so i'm confused why need collection serializer.
to start answer "why need collection serializer",
i have controller methods return "unsorted" lists so:
[ { id: 2, collection_id: 3, text_choice: "cover" }, { id: 3, collection_id: 3, text_choice: "remix" }, { id: 4, collection_id: 4, text_choice: "studio" } ]
but want return these entries grouped collection_id
, respond json this:
[ { "3": [ { id: 2, text_choice: "cover" }, { id: 3, text_choice: "remix" } ] }, { "4": [ { id: 4, text_choice: "studio" } ] } ]
(sorry formatting: doing "blind" kind of difficult sublime text user ;) )
so arrived @ conclusion needed implement own collectionserializer
transformation. you, couldn't find example; actually, you've given advanced answer there moment ! i'll try play start , create issue on gh.
i'll update if anything...
to anybody: feel free if know more !!
Comments
Post a Comment