title: Rails, creating a tree like structure. JSON output
body: <p>i have a table of data, which can have each other as parents or childs, its handled with the field parent_id (I am using the act_as_tree gem)</p>
  
  <p>the first level items have 0 as parent_id </p>
  
  <p>there could be an infinite number of children. I want to output as JSON. 
  the final output shud be something like this </p>
  
  <pre><code>{
     "feild1": "dd",
     "filed2": "ee",
     "child" : {
         "feild1": "dd",
          "filed2": "ee",
          } 
     "child" : {
          "feild1": "dd",
          "filed2": "ee",
                   "child" : {
                        "feild1": "dd",
                        "filed2": "ee",
                    } 
          } 
  
  }
  </code></pre>
  
  <p>so far i all i have is this</p>
  
  <pre><code>def coa_tree
     @roots = Coa.find(:all, :conditions =&gt; ['parent_id = ?', 0])
     @response = @roots
  
     @roots.each do |root|
       logger.debug "roots each"
        output = root
        root.children.each do |child|
             output = {:child =&gt; output, :child =&gt; child}
  
        end
  
     end
     respond_with(@response)
  end
  </code></pre>
  
  <p>clearly i haven't even come close to solving the problem. If someone could point me to the right direction i would really appreciate it. maybe there is a plugin that i don't know about that would help me solve this.
  thanks.</p>