title: How to render all records from a nested set into a real html tree
body: <p>I'm using the <a href="http://github.com/collectiveidea/awesome_nested_set" rel="noreferrer"><code>awesome_nested_set</code></a> plugin in my Rails project. I have two models that look like this (simplified):</p>
  
  <pre><code>class Customer &lt; ActiveRecord::Base
    has_many :categories
  end
  
  class Category &lt; ActiveRecord::Base
    belongs_to :customer
  
    # Columns in the categories table: lft, rgt and parent_id
    acts_as_nested_set :scope =&gt; :customer_id
  
    validates_presence_of :name
    # Further validations...
  end
  </code></pre>
  
  <p>The tree in the database is constructed as expected. All the values of <code>parent_id</code>, <code>lft</code> and <code>rgt</code> are correct. The tree has multiple root nodes (which is of course allowed in <code>awesome_nested_set</code>).</p>
  
  <p>Now, I want to render all categories of a given customer in a correctly sorted tree like structure: for example nested <code>&lt;ul&gt;</code> tags. This wouldn't be too difficult but I need it to be efficient (the less sql queries the better).</p>
  
  <p><strong>Update:</strong> Figured out that it is possible to calculate the number of children for any given Node in the tree without further SQL queries: <code>number_of_children = (node.rgt - node.lft - 1)/2</code>. This doesn't solve the problem but it may prove to be helpful.</p>