Rails two objects in one json object
I have tree models: Tours开发者_运维百科, buses and locations Tour have more buses, buses more locations.
I want to return json object with buses last locations for some tour.
Pseudo example:
BUSES
BUS1
last_location
/BUS1
BUS2
last_location
/BUS2
BUS3
last_locaiton
/BUS3
/BUSES
tour.buses will give me buses for that tour, and bus.locations.last latest location.
I know that is no since fiction, but I don't know how to pack this in one json object to return it :|
You could do it like this:
both_pieces = {
:tour => tour,
:last_location => bus.locations.last
}
And then send both_pieces
back as JSON. Then the client would get one JSON object with the tour
and last_location
objects inside it.
精彩评论