1

I'm trying to use include for my json response, which works great, but when I start to nest it, I'm getting a strange result

The include doesn't return the gym object. If I flip the order and put trainer last, that object doesn't get returned.

Am I missing something with my syntax ?

msg = {:requests => @customer_sessions.as_json(:include => [:session_coupon, :trainer => {:methods => [:rating, :completed_sessions_length, :following]}, :gym => {:methods => :rating}])}
format.json  { render :json => msg }
3
  • 1
    quick idea, try a hash instead of an array to :include. EG: :include => {:one, :two, :three} Commented Apr 29, 2015 at 21:22
  • @Rots I don't think that works, but I understand your idea. Hashes need a key value pair right ? Commented Apr 29, 2015 at 21:29
  • 1
    Yeah that's right. Was thinking more like this :include {:trainer => {:methods => [:rating, :completed_sessions_length, :following]}, :gym => {:methods => :rating}} Commented Apr 29, 2015 at 21:34

1 Answer 1

1

Adding hashes worked for those interested

msg = {:status => "SUCCESS", :messages => "Requests Found", :requests => @customer_sessions.as_json(:include => [:session_coupon, {:trainer => {:methods => [:rating, :completed_sessions_length, :following]}}, {:gym => {:method => :rating}}])}
format.json  { render :json => msg } # don't do msg.to_json
Sign up to request clarification or add additional context in comments.

Comments

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.