dormitkon
dormitkon

Reputation: 2526

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 :|

Upvotes: 1

Views: 247

Answers (1)

mu is too short
mu is too short

Reputation: 434985

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.

Upvotes: 1

Related Questions