title: Multi level categories in Ruby on Rails and Active Record associations
body: <p>Ineed to create multilevel categories in Ruby on Rails. So I create a model Category which has title and description and has many articles.</p>
  
  <pre><code>class Category
    has_many :articles
  end
  </code></pre>
  
  <p>Then I need to add <code>parent_id</code> field to <code>Category</code> model. This field must be either null (if it's a parent category) or has some id (if it's a child category). Obviously, to select any parent category it has to select <code>Select * from Categories where parent_id=null</code>. </p>
  
  <p>I hope you understand what I mean.</p>
  
  <p>How can reach it?</p>
  
  <p><strong>UPDATE</strong>: Thank you for your suggestion. Here is what I have</p>
  
  <pre><code>class Category &lt; ActiveRecord::Base
    belongs_to :parent, :class_name =&gt; "Category", :foreign_key =&gt; "parent_id"
    has_many :children,  :class_name =&gt; "Category", :foreign_key =&gt; "parent_id"
    attr_accessible :description, :title
  end
  </code></pre>
  
  <p>As I understood, <code>:foreign_key =&gt; "parent_id"</code> in <code>has_many :children</code> has to be removed, right?</p>