I’ve implemented as_json in the parent model as follows:
def as_json(options = {})
options[:include] = :items
super(options)
end
include_root_in_json = true is set in the configuration.
What I GET is:
[
{
"order": {
"items": [
{
"key1": "value1"
},
{
"key1":"value2"
}
],
"key1": "value1"
}
}
]
But what I WANT is this:
[
{
"order": {
"items": [
{
"item": {
"key1": "value1"
}
},
{
"item": {
"key1": "value2"
}
}
],
"key1": "value1"
}
}
]
So the root name is not included for nested associations. Is that a bug or am I missing something?
As far as i can tell “include_root_in_json” does not work for nested attributes but only on the very root like:
For this example it would remove the “videos” root.
Tip
I found that as_json is not really good if you are building something like an API where you need to be very flexible sometimes. For that reason i am using RABL, mabye you should give it a try https://github.com/nesquena/rabl