← Back to Upcase

STI and Inheritance in a Rails project

(Justin Gordon) #1

Any guides on when and when not to use STI and inheritance in ruby projects?

I used to use inheritance tons in the Java world, but I find I tend to only use modules in ruby.

I’m thinking that I should be using inheritance more so that my code does more tell rather than ask…and thus has less conditionals.

And should a class broken up into 2 small classes with a small base class stay in one ruby file, or break up into 3 files?

(Derek Prior) #2

I would consider using STI when the state of the models involved is identical but the behavior is not. STI is not a great fit when you start to add columns hat apply only to a subset of the models involved.

To be clear, both included modules and superclasses are forms of inheritance. Neither are a “tell, don’t ask” silver bullet.

When at all possible, I would advise you to favor composition over inheritance. See also the notes on our recent dev discussion at thoughtbot.

I keep just about all classes in their own files.