BigDaddy
BigDaddy

Reputation: 11

Chaining models while rendering JSON in rails

How can I chain multiple models to be rendered as a JSON object in rails. Currently I have a render statement like

render json: current_user.role.selected_bids.to_json(include: [:project => {include: [:milestones , :skill_category] } ] )

I want to append to this JSON object another model where I get to include a model associated to :milestones. Something like this

render json: current_user.role.selected_bids.to_json
(include: [:project => {include: [:milestones=> {include: [:timetrackers]}, 
:skill_category]}])

but its throwing a syntax error. Is it possible to do this level of nesting or should I make another API call ?

Upvotes: 0

Views: 153

Answers (1)

bitsapien
bitsapien

Reputation: 1833

The reason you are getting a syntax error is because you are trying to create a Hash using syntax you would use to create an Array. You can instead do this something like this:

render json: current_user.role.selected_bids.to_json(
  include: [
    project: { 
      include: [  
        {
          milestones: {
            include: [:timetrackers]
          }, 
        },
        :skill_category
      ]
    }
  ]
)

Upvotes: 1

Related Questions