Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails JSON multiple nested associations

I have a list of objects, tests, which contain questions and bonus questions, each of which has a subject model. I'm trying to include all of them in a JSON API but I keep getting odd incomprehensible syntax error messages. I can get it working with questions and subject or bonus questions and subjects, but not both. Here is what I have now:

render json: tests.as_json(:include => {:questions =>{:include => {:subject}},:bonuses => {:include => {:subject}}})

The error message I get is

/app/controllers/test_controller.rb:49: syntax error, unexpected '}', expecting => ...ssups =>{:include => {:subject}},:bonuses => {:include => {:... ... ^ /app/controllers/question_set_controller.rb:49: syntax error, unexpected '}', expecting => ...uses => {:include => {:subject}}}) ... ^ /app/controllers/question_set_controller.rb:76: syntax error, unexpected end-of-input, expecting keyword_end

Thanks in advance.

like image 575
Simon Means Avatar asked Dec 19 '22 11:12

Simon Means


1 Answers

You have extra pair of braces wrapping a single symbol. That is not valid Ruby hash syntax. It should be

render json: tests.as_json(:include => {:questions => {:include => :subject}, :bonuses => {:include => :subject}})
like image 66
BroiSatse Avatar answered Dec 27 '22 12:12

BroiSatse