pedalpete
pedalpete

Reputation: 21546

return json from deeply nested activerecord object

I've got some fairly deep nesting going on in my models where I have

-user
   -user_event
        -event
              -location
              -categories
              -attendees

I'm trying to return a json object which has all the nested elements returned. I can get to

user = User.find(current_user.id).user_event #or ,:includes => :user_event
render :json => user

but I don't see how I can display the entire hash in one go.

Upvotes: 2

Views: 1693

Answers (1)

Azolo
Azolo

Reputation: 4383

try this

render :json => user.as_json(
                  :include => { :user_events => {
                    :include => { :events => {
                      :include => [:location, :categories, :attendees]
                    } }
                  } }
                )

Documentation for Serializers::JSON::as_json

Upvotes: 5

Related Questions