← Back to Upcase

Comparing STI (Single Table Inheritance) to Concerns


(Justin Gordon) #1

Any suggestions on when and when not to use STI vs. Concerns?

  1. Concerns. Use multiple tables/models, with same columns names for common attributes, and put common functionality into shared concerns.
  2. STI. Use single table with type column. Put common functionality in base class (AR class that maps to table name), and put distinct functionality into child classes.

Here’s a couple good articles I found on STI:

  1. How (and When) to Use Single Table Inheritance in Rails
  2. Single Table Inheritance - Problems and solutions

(rubylove.io) #2

How awesome is that, you put together 2 of my most disliked features of rails :wink:

STIs require a doctor’s attention… and Concerns are well, concerning!

But seriously, to me an AR::Model is just a database layer, nothing more. It is not a bucket for business logic. That should be in a Ruby class or module, or more likely, many.

As far as STI, I prefer polymorphic associations. But I am also a fan of composition over inheritance, a preference I think Thoughtbot shares. STI screams inheritance and you end up with a bunch of useless columns.

So my in my opinion when should you use Concerns or STI? Never and never :slight_smile: Do that shit with composition and stop being so infatuated with the database layer. It is an unhealthy obsession that leads to state, bugs, and slow tests. :blush: :tada: :balloon: