I find that the interactivity that a JavaScript single-page application provides makes it quite easy to build up complicated nested objects on the client side.
For example, in an application I'm writing, I have a Backbone destination and origin models, a route which connects them and then a bus which travels that route. All of this comes together quite naturally on the client side.
# Bus toJSON()
{
seats: 45,
route: {
summary: "a route summary",
origin: {
latitude: 45.654634,
longitude: 23.5355
},
destination: {
latitude: 45.654634,
longitude: 23.5355
}
}
}
However, I find that when it comes time to persist my bus (the user is ready to save everything), the rails models accepts_nested_attributes_for method makes things quite ugly. I end up having to send data to the server which looks like
{ "bus_route_attributes_origin_attributes_latitude" => "45.654634" }
in order to get ActiveRecord to play nicely.
How should I change my server side to allow me to deal in JSON more easily?