Rails Single Table Inheritance

Ruby on Rails supports Single Table Inheritance. Single Table Inheritance allows you to have one single SQL table, such as an employees table, manage different type of employees like managers and developers. A manager would normally have more responsibilities than a developer and you can separate these additional responsibilities in a different ActiveRecord model class.

Here is another example of Single Table Inheritance that you might find useful. You might have to support a hierarchy of users for you web application, such as User, Editor, and Administrator. An Administrator might have full Create, Read, Update, and Delete (CRUD) access while a User might just have read access. For this hierarchy of users you will need to create a Ruby class for each type and place it in the Rails app/models directory. Here is the base User class:

My Editor class:

And the Administrator class:

Place each of these classes in their own Rails model file under the app/models directory.

To indicate to Ruby on Rails that the users table needs to support Single Table Inheritance you need to add a column named ‘type’ to the users table. Here is my users table definition:

In the column named type you should store the name of the class, the class type, that should be used for each user. To mark an certain user as an admin set his type to ‘Administrator’. By setting a user’s type to ‘Administrator’ you are giving him full administrator privileges as defined in your Administrator model class.

And now, in your controller you can look up all users that are administrators by using the following bit of code:

If you want to look up all known users you can do so by using the User class as in the following snippet of code:

At this point, you would add additional responsibilities to editors and administrators by adding methods to those model classes.

Technorati Tags: , , , ,

Query Managed Auto Increment »
« JavaOne Brown Bag Session
 
Related Posts
Recent Posts
 

4 Comments so far

  1. Kapslok on April 1st, 2007

    A quick question: would you still have controllers for Administrator, Editor and User, or just a single one for User? This stuff is confusing. Is it possible to have an ActiveRecord model inherit from another ActiveRecord model - so as an example you extend the original User model with additional fields in a Editor model?

    On a side note:
    Your db table definition will restrict you user.type to 10 characters, so your example of specifying ‘Administrator’ will fail.

  2. TechKnow on April 2nd, 2007

    @Kapslok - Yes, an Administrator model can extend the User model. The name of the database table would be users. The users table would store both administrator and regular end users.

    In general, a model can extend from any other model or from ActiveRecord directly. The name of the database table would be the plural of the class that extends ActiveRecord directly.

    A model does not have to be paired with a controller so you don’t have to have a controller for each model.

    Thanks, for letting me know about the the column type being 10 character wide. I originally named the administrator class Admin.

  3. James Darling on August 10th, 2007

    Thanks for this, I’ve been doing some rather confusing Single Table Inheritance stuff for the first time recently, and this has been (and will continue to be) a very good resource.

  4. Chemica on December 14th, 2007

    Crisp, clear and to the point. Ever thought of writing for the RoR Wiki? ;)

Leave a reply