ActiveScaffold: Single Table Inheritance (STI)


Today, I would like to show you an improved way to manage Single Table Inheritance with ActiveScaffold. It s a real time safer.
STI Models should hopefully have a big overlap of attributes, which is the base assumption for my implementation of STI in ActiveScaffold.

Basically, we will show an index view of parent sti controller, in which member actions such as edit, show,delete are linking to corresponding specific sti controller. Create link opens a popup menu showing the different sti models.

That s the concept let s try it out. I will use an example from the book: “Agile Web Development with Rails” written by Dave Thomas and David Heinemeier Hansson.
That example consist of the following models:

  1. Person (sti_parent)
  2. Customer
  3. Employee (Parent of Manager)
  4. Manager

It s quite tedious to set up an application with these models, therefore we will shorten that process.

Setup Example Application

  1. Download: one_step_active_scaffold
  2. Download: sti_blog.rb and rename it to model_setup.rb
  3. Execute following command: ruby one_step_activescaffold.rb sti jquery

Great, we ve prepared our example application, now we have to configure our controllers

Controller Inheritance

ActiveScaffold configuration can be inherited, which is quite useful for sti, because many columns are shared.

class CustomersController < PeopleController
class EmployeesController < PeopleController
class ManagersController < EmployeesController

Definition of column set for each Controller

We have to define for each controller, which columns we would like to use. Please note that we do not have to define anything specific for manager controller because it should use same settings as employee controller.

class PeopleController < ApplicationController
  active_scaffold :person do |conf|
    conf.columns = [:type, :name, :email]
class CustomersController < PeopleController
  active_scaffold :customer do |conf|
    conf.columns = [:type, :name, :email, :balance]
    conf.update.columns = [:name, :email, :balance]
    conf.create.columns = [:name, :email, :balance]
class EmployeesController < PeopleController
  active_scaffold :employee do |conf|
     conf.columns = [:type, :name, :email, :boss, :department]
     conf.update.columns = [:name, :email, :boss, :department]
     conf.create.columns = [:name, :email, :boss, :department]

Parent Controller STI Child Models

One last step, we have to tell the parent controller, which sti_models he should be able to manage.

class PeopleController < ApplicationController
  active_scaffold :person do |conf|
    conf.sti_children = [:employee, :customer, :manager]

Our Application is ready, start the server and go to: localhost:3000/people.

Hopefully, you will benefit as much as I did.

ActiveScaffold Gem

Leave a comment

Yes, ActiveScaffold is a available as gem. I think it s a great step forward and it will be easier than ever to integrate activescaffold into your projects. Finally, we ve managed to get it up and running and I would like to thank Chris Shoemaker for his contribution to this project.

Let me introduce to you the different options you have to integrate activescaffold into your project:

  1. Plugin
    rails plugin install git:// -r 'rails-3.0'
  2. Gem
    gem active_scaffold_vho
  3. Gem linked to git repository using rails 3.0
    gem 'active_scaffold_vho', :git => 'git://', :branch => 'rails-3.0'
  4. Gem linked to git repository using rails 3.1 (not working)
    gem 'active_scaffold_vho', :git => 'git://'

I ve updated One-Step-Install to use third option as a default and if you do not have git installed it will use second option.

All activescaffold plugins have been gemified as well:

ActiveScaffold: jquery 1.4.4 supported


Just a short note that ActiveScaffold might be used with jquery 1.4.4 since today.

If you use setup generator your application will be automatically configured to use jquery 1.4.4.

If you would like to update your existing activescaffold application please read following guide:

Change application.html.erb

<%= stylesheet_link_tag '' %>
<%= javascript_include_tag '' %>
<%= javascript_include_tag '' %>

Update rails.js

Replace existing one in public/javascripts by

That s it. Please note that you ve just included non minified versions of jquery.

In case of any issue… please open an issue.