So in my rails app there are Lists. Each List has_one Item. Each Item has_many Items. So an Item can belong_to another Item. I want to render these resources as JSON where each Item has its child Items nested under it. Since each Item could potentially have child Items of its own, I can't just keep using the include: option because I would not know when the last Item has no children.
So it seems like I would somehow have to use recursion while rendering the JSON, so if an Item has children it's children are nested under it, and if any of those children have children, their children are nested under them.
I am not sure how to do this using to_json and the include option. If I was just rendering the first child Item of the List I would just do this:
respond_to do |format| format.json { render json: @list.to_json(include: :item) } end
But I have no idea how to recursively render the Item children of the :item and their children, and so on.
Another answer suggests using a view object or a gem like RABL for a different problem that is also seems to be too complex for to_json. Is what I'm trying to do too complicated for the to_json method? What is my best option to recursively render child resources as json?
Thanks for the help.