So we all know that rails’ STI (single table inheritance) is icky because it leads to a cluttered data model and suboptimal database.
However PostgreSQL seems to handle inheritance quite beautifully!
Is there a way to get rails’ nice clean STI API while utilizing Postgres inheritance instead of painfully wide tables and “type” columns?
In short – no there no nice clean STI API for what you are trying to accomplish as of right now.
I actually looked into that a about a year ago and came to a conclusion that it is not a good idea for several reasons:
Most IT problems really come down to this: Effort vs Benefit.
In your case you should ask yourself this question:
structure if it will only speed up your raw SQL query by a few seconds? Maybe it’s better to write a more explicative SQL query? Most applications don’t grow to the size where it really becomes an issue. But it maybe different in your case.
P.S.:
Also a quick tip on structuring STI in your app: If you find that you have a lot of models that use STI like a ProductCategory, CommentCategory, PhoneCategory, ClientCategory that all inherit from Cateogory – I tend to organize them in folders inside model directory. Then in application.rb just add a line:
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/**}')]