开发者

Rails two objects in one json object

开发者 https://www.devze.com 2023-03-28 02:23 出处:网络
I have tree models: Tours开发者_运维百科, buses and locations Tour have more buses, buses more locations.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消