In my previous post I wrote about trying to get class table inheritance working in Rails. I passed by the hack and went with the plugin which ended up not really working right. I revisited the class table inheritance hack and actually got it working. I pretty much just dumped that code into a plugin so all you have to do is:
spaghetti.rb
[source language=":ruby"]
class Spaghetti < Noodle
class_table_inheritance
end
[/source]
noodle.rb
[source language=":ruby"]
class Noodle < ActiveRecord::Base
end
[/source]
One thing that did trip me up at first was that the spaghettis table can’t have an ‘id’ column for a primary key or you end up with some strange stack overflow errors. The noodles table also needs a ‘type’ varchar column to store the class names of the rows its storing. Even pagination works when you explicitly state what tables you’re conditions are using as in:
[source language=":ruby"]
@spaghetti_pages, @spaghettis = paginate :spaghettis,
:conditions => [ "noodles.length_in_inches = (?)", 10],
:per_page => 10
[/source]
Download the class_table_inheritance plugin