title: How to Structure Multiple Levels of Hierarchy in Ruby on Rails?
body: <p>A rails app contains many different Pages of content. The Pages are organized into small groups known as Sections: </p>
  
  <pre><code>class Page &lt; ActiveRecord::Base
     attr_accessible: section_id #etc..
     belongs_to :section
  end
  
  class Section &lt; ActiveRecord::Base
    attr_accessible :title #, etc... 
    has_many :pages
  end
  </code></pre>
  
  <p>The Sections need to be organized too, but what is the best way to do this - re-use sections themselves, or create a new Unit model? </p>
  
  <p><strong>Option 1 - Re-use Section</strong><br>
  Allow Section to have children and parent Sections. That way, you don't need to create another model with similar fields as Section. Some sections will have_many pages, and other sections will have_many children sections:</p>
  
  <pre><code>class Section &lt; ActiveRecord::Base
    attr_accessible :parent_id  :title # etc... 
    has_many :pages
  
    belongs_to :parent, class_name: "Section"
    has_many :children, class_name: "Section", foreign_key: "parent_id"
  end
  </code></pre>
  
  <p><strong>Option 2 - new Unit model</strong><br>
  Create another model called Unit to organize the sections. It will have many similar fields to section, but it will be a clearly separate entity. </p>
  
  <pre><code>class Section &lt; ActiveRecord::Base
    attr_accessible :title, :unit_id # etc... 
    has_many :pages
    belongs_to :units
  end
  
  class Unit &lt; ActiveRecord::Base
    attr_accessible :title # etc... 
    has_many :sections
  end
  </code></pre>
  
  <p>The advantage of Option 1 is it avoid some duplication, and could be adapted in the future if even more levels are needed. However, Option 2 clearly separates the roles of Sections, which have_many pages, from Units, which have_many Sections, which can help keep other code clear. Which is the best approach?</p>
  
  <p><strong>Update</strong><br>
  It seems Option 2 would have clearer code, such as when going through all the Sections. Is it worth re-using Sections if it would make some code more complicated? For example, here's how to list all the Sections in an organized manner:</p>
  
  <p>Option 2 - For each Unit, list all the child sections. Then list any Sections that aren't in any Unit.</p>
  
  <p>Option 1 - For each parent Section, list all the children Sections.  Then list any Section that has with no parent Section or child Section. </p>