Reputation: 3400
I am trying to generate JSON using RABL, my index.rabl
view looks like this:
collection @products => :products
attributes :id, :name, :price, :category_id
node(:total) {@products.count}
This generates JSON with the following structure:
{"products":[{"id":1,"name":"product name","price":0.00,"category_id":1,"total":30},
{"id":2,"name":"product2 name","price":0.00,"category_id":1,"total":30},...]}
However, i would like the structure of the generated JSON to look like this:
{ "products":[{"id":1,"name":"product name","price":0.00,"category_id":1},
{"id":2,"name":"product2 name","price":0.00,"category_id":1},...],
"total":30
}
This means i would like to get the "total":30
out of the "products"
array and put it into the root object of the generated JSON. What changes do i need to make to my view file to generate the required JSON? I have very little experience with RABL and help will be highly appreciated.
Upvotes: 0
Views: 447
Reputation: 4596
In your index.json.rabl you can do this:
object false
child @events do
attributes :id, :message
end
node(:count) { @events.size }
The result is:
{
"count": 50,
"events": [
{
"id": 124,
"message": "Hola"
},
{
"id": 123,
"message": "Chau"
},
{
"id": 122,
"message": "Yeah baby!"
}
]
}
I've tested this on my project. You can change events with products...
Upvotes: 1