Hello: Does anyone have a solution to convert an acts_as_tree model to json that includes all its nodes? I trued using :include=>:children, but that only goes down to one single level. Any idea how to iterate the whole tree?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a helper (or private method) which is recursive and turns your nested models in to a nested hash, then use
to_jsonto generate a json string.I needed to generate Json in a HTML/Erb view, but the same idea should apply if you need to generate it from a controller action, or use an Erb template to generate Json. I also needed a (hardcoded) root node, you can skip that if its not required.
categories.html.erb
categories_helper.rb