Question

I recently came across a condition were i wanted to send two objects in the RABL as a response.

[
 {
   id: "1",
   name: "XYZ"
 },
 {
   id: "1",
   name: "XYZ"
 },
 {
   total: "2"
 }
]

All I could manage was this , which is not correct.

[
 {
   id: "1",
   name: "XYZ",
   total: "2"
 },
 {
   id: "1",
   name: "XYZ",
   total: "2"
 }
]

I Found a solution which was to use a partial to iterate on the object and just add a new

node(:name) {partial("users/names", :object => @users)}
node(:total){ @total}

This is a hack, which i don't want because it wraps all the names in a node . Is there any other way to do it ?

Was it helpful?

Solution

In your rabl file try this:

child @users, object_root: false do
    attributes :id, :name
end
node(:total) { @users.size }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top