ActiveRecord provides a single-table inheritance model. Child classes are automatically persisted in the parents table using the "type" column (just a varchar) to figure out which class the data belongs to.
Obvious but to be Noted: the example isn't recursive.

h2. Database schema (MySQL)

<pre><code>CREATE TABLE `topics` (
  `id` int(11) NOT NULL auto_increment,
  `title` varchar(255) default NULL,
  `author_name` varchar(255) default NULL,
  `author_email_address` varchar(255) default NULL,
  `written_on` datetime default NULL,
  `last_read` date default NULL,
  `content` text,
  `approved` tinyint(1) default 1,
  `reply_count` int(11) default NULL,
  `parent_id` int(11) default NULL,
  `type` varchar(50) default NULL,
  PRIMARY KEY  (`id`)
);</code></pre>

h2. Model code

<pre><code>
require 'active_record'
class Topic < \ActiveRecord::Base
  has_many :replies, :foreign_key => "parent_id" 
  protected
    def before_destroy
      replies.delete_all
    end
end
</code></pre>
 
<pre><code>
class Reply < Topic
  belongs_to :topic, :foreign_key => "parent_id" 
end
</code></pre>

h2. Associations in the above model

!http://media.nextangle.com/rails/inheritance.gif!

category:Example
