Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6378249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:58:51+00:00 2026-05-25T01:58:51+00:00

Hello: Does anyone have a solution to convert an acts_as_tree model to json that

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:58:52+00:00Added an answer on May 25, 2026 at 1:58 am

    Use a helper (or private method) which is recursive and turns your nested models in to a nested hash, then use to_json to 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

    <script type="text/javascript">
      $(function(){
          var json = {
            id: "node00",
            name: "New Pathway",
            data: {},
            children: 
    <%= @categories.select { |c| c.root? && !c.leaf? }.collect { |c| category_to_spacetree_json(c) }.to_json.html_safe %>
          };
    
          init(json);
      });
    </script>
    

    categories_helper.rb

      def category_to_spacetree_json(category)
        hash = {
          :id => category.id,
          :name => category.name,
          :data => '',
          :children => []
        }
    
        unless category.leaf?
          hash[:children] = category.children.public.collect { |c| category_to_spacetree_json(c) }
        end
    
        hash        
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have a hello world sample or tutorial for creating an Eclipse plugin
If I have 2 files each with this: Hello World (x 1000) Does that
Hello does anyone have a code example of how I can time bomb an
Hello everyone I'm trying to work with jboss messaging, does anyone knows the default
Hello is there a class that does a pretty conversion?
Does anyone know, or have a link to an article or a step by
Does anyone have any example implementation of making individual object props readOnly/non-configurable? I mean
I have a solution from an earlier post that was kindly provided by Dimitre
Does anyone have any suggestions for a good way to call R from S-Plus?
Does anyone have any quality (and up-to-date) information regarding sending mail via Gmail using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.