ruby - How do I "group" sets in Rails' jbuilder? -


i've got set of "player" , "team" models in ror app (a json api) representing sports team, "roster" model acting middleman in many-to-many relationship. in "roster" view, i'm trying 'group' players team jbuilder, can't figure out proper code this.

my current jbuilder view code (index.json.jbuilder) looks this:

json.rosters @rosters |roster|     json.team_id       roster.team.team_id     json.team_name     roster.team.team_name     json.player_id     roster.player.player_id     json.first_name    roster.player.first_name     json.last_name     roster.player.last_name end 

this outputs following json:

{     "rosters": [         {             "team_id": "1",             "team_name": "dallas mavericks",             "player_id": "1",             "first_name": "dirk",             "last_name": "nowitzki"         },         {             "team_id": "1",             "team_name": "dallas mavericks",             "player_id": "2",             "first_name": "rajon",             "last_name": "rando"         }     ] } 

my question: how can jbuilder output data in format instead? :

{     "rosters": [         {             "team_id": "1",             "team_name": "dallas mavericks",             "players": [                 {                     "player_id": "1",                     "first_name": "dirk",                     "last_name": "nowitzki"                 },                 {                     "player_id": "2",                     "first_name": "rajon",                     "last_name": "rando"                 }             ]         }     ] } 

some side notes:

  • for wondering why team-player relation many-to-many, it's because players allowed change teams. roster table has start- , end-date fields represent player's tenure on team, representing changing roster on time; isn't shown in above example lack-of-clutter's sake.
  • i more in team view, don't want return roster information there since it's used listing teams. might end being lot of data unconditionally pull if include roster stuff each time.
  • i'm using sequel orm, not activerecord, if solution involves twiddling source data before sending jbuilder, i'm not sure if ar magic apply. still rather new this.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -