This is similar to include root in json for array? , but slightly different.
I have an object Alpha with a has_many association to Beta. I'm rendering Alpha as JSON and want to include Beta.
@alpha.as_json(:include => :beta)
This works great, and since I have include_root_in_json set to true, the root of alpha is outputted.
{"alpha":
{
"created_at":"2012-06-07T23:37:30Z",
"id":1,
"beta":[
{
"created_at":"2012-06-11T23:46:58Z",
"id":1,
}
]
}
}
I want to include the root in the beta array as well, but I can't seem to find a way to do this.
尝试将关联的root
选项设置为true
:
@alpha.as_json(:include => { :beta => { :root => true } })
My solution was to use RABL (https://github.com/nesquena/rabl). It provides better formatting control for JSON.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.