I would like to get the following JSON.
[{"Product":{"CountryName":4848, }},{"Product":{"CountryName":700}]
module API
module Entities
class Example < Grape::Entity
expose(:product) do
expose(:country_name) do |product, options|
product.country.name
end
end
end
end
end
The parameters are product and name. I would like to return Product, ProductName.
How to apply alias for looping elements in grape entity framework?
Productattribute : use a'Product'string instead of the:productsymbol,CountryNameattribute : you need to create a method in your entity that will returnproduct.country.nameand expose it in your entity. And then, use an alias so that the key will beCountryNameas expected (you can see grape-entity documentation about aliases if need be).In your case, this would give :
And in your endpoint, you specify the entity that must be used to serialize the Products instances. In my example, you may have noticed that I took the liberty of renaming the entity to
Product, which would give us in the endpoint:Which should get you this kind of output, as expected: