I have a table named featured_products witch contains only two columns (product_id, position). On my GET /featured_products, I want to render something like this
[
{ "name":"Product 1" }
{ "name":"product 2" }
]
But instead, I logically get this:
[
"product":{ "name":"Product 1" }
"product":{ "name":"product 2" }
]
Following the doc of Active Model Serializers, I'v tried these in my featured_product serializer class:
embed_in_root: true
and
belongs_to :product, embed_in_root: true
but the first on gives an error while second changes the JSON in no way.
I don't know if I missed the answer I'm looking for in Active Model Serializer's doc or if the answer is to be found elsewhere, but I didn't manage to solve this on my own, I'd be happy to get some advices here.
Thanks
You can also try this:
render json: FeaturedProduct.includes(:product).order(:position).map(&:product) # include :product to avoid N + 1 queries on products