This is a follow-up to this question: JSON is not nested in rails view
I have an application that has a heirarchy of models, each 1:many as they descend. At the second from bottom level is a model called LessonLayout and it has two children, with a 0:many relationship possible. The children are called LayoutField and LayoutTable.
So far I have this code listed below (with the help of SO user meagar).
def show
@course = Course.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json do
render :json => @course.to_json(:include => { :units => { :include => {:lessons => {:include => { :lesson_layouts => { :include => :layout_fields, :include => :layout_tables}}}}}})
end
end
end
A lesson layout with have either a :layout_fields or :layout_tables object. At present it produces the :layout_tables data, but does not display the :layout_fields. If I reverse the objects in the ruby code, the converse happens. I would like to see both returned in the JSON.
I’ve tried playing around with the code but am not getting anywhere. Thanks in advance.
I worked it out. Pass the two peer objects in an array, like this: