Single table inheritance and where to use it in Rails

Characterising STI as mostly useful when attributes are the same but behaviour differs is “about right”, but possibly a little limiting. I like to use STI when there is, as the name suggests, a clear OO-style inheritance relationship, rather than the database-style relationship between objects of different types.

If there is common code between bots and users, I’d say STI sounds like a winner. If there’s just some common attributes, it’s probably less applicable but still worth having a go at.

I’m a pretty experimental person, so my recommendation is to give it a go. Branch your code and refactor the models into an STI relationship. See if it really does dry things up, or just swaps one set of headaches for some other problem.

One thing I think you won’t see much benefit from is drying up your controllers. In my experience, STI models don’t often translate into similarly related controllers. But that would be something else to experiment with. Sometimes there’s a win, sometimes there isn’t.

Leave a Comment