Reputation:
RABL Current Code :
object @region
attributes :id, :name, :latitude, :longitude, :region_id
child :sub_regions do
attributes :name
end
I get the following output :
{
"id": 1,
"name": "test",
"latitude": "30.932351",
"longitude": "92.83391",
"region_id": 1,
"sub_regions": [{
"name": "1"
}, {
"name": "2"
}, {
"name": "3"
}, {
"name": "4"
}, {
"name": "5"
}, {
"name": "6"
}, {
"name": "7"
}
]
}
I want to remove name attribute and just display the values as an array But what I want is to convert the collection to an array like the output here :
{
id: 1,
name: "test",
latitude: "30.932351",
longitude: "92.83391",
region_id: 1,
sub_regions: [
"1",
"2",
"3",
"4",
"5",
"6",
"7"
]
}
The closest I can get to this is by this RABL code :
code :sub_regions do |s|
s["name"]
end
{
{
id: 1,
name: "test",
"latitude": "30.932351",
"longitude": "92.83391",
region_id: 1,
sub_regions: "1"
}
}
By the above code block in show.json.rabl it displays only the first element, how do i make it an array with all the elements?
Upvotes: 1
Views: 1582
Reputation: 530
joe nayyar had the right idea in his answer, but there's a cleaner way than using object false
.
child :sub_regions do
attributes :name
end
becomes...
node :sub_regions do |region|
region.sub_regions.collect(&:name)
end
Upvotes: 2
Reputation: 566
For anyone lazy roaming around for solution like me, as RABL is on top of the ruby code base you can do something like:
object false
node :region, :object_root => false do
{:id => @region.id, :name => @region.name, :latitude=> @region.latitude, :region_id => @region.region_id, :sub_regions=>@region.sub_regions.collect(&:name)}
end
Upvotes: 0
Reputation: 256
It is because you used object @region when you should have used collection @region
Upvotes: 0