Class table inheritance in Ruby on Rails

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

noodle.rb

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:

Download the class_table_inheritance plugin

3 Responses to “Class table inheritance in Ruby on Rails”

  1. GonzoTanuki Says:

    Hi, i have a problem with the plugin.
    I have:

    table publication:
    id
    title
    author
    type

    table book:
    publication_id
    editor

    publication.rb
    class Publication

  2. GonzoTanuki Says:

    class Publication

  3. GonzoTanuki Says:

    sory, the minor signus is not allowed…
    i have this:

    publication.rb
    class Publication ‘inherits’ ActiveRecord::Base
    end

    book.rb
    class Book ‘inherits’ Publication
    class_table_inheritance
    end

    When i do, book/show or book/list only shows the attributes of Publication.
    There is something that i’m doing wrong???

    Thanks. Gonzalo

    Sory if my english is a little poor…

Leave a Reply

You must be logged in to post a comment.