2

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.

1 Answer 1

5

try this

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

Documentation for Serializers::JSON::as_json

Sign up to request clarification or add additional context in comments.

1 Comment

I think this is really close Azolo, unfortunately now I'm getting the error undefined method macro' for nil:NilClass` which I assume is because i don't always have a category, or location or attendees in each event. Any way around that? I don't see it in the doc you referred to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.