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:
- If you want to utilize PostgreSQL specific features - you are basically marrying yourself to that DB. Don't get me wrong PostgreSQL is a great DB and I've used it on a number of occasions, but you are going to be stuck with that DB and app design.
- Most likely if you start using DB specific features you'll either end up implementing them manually ( running some kind of commands on the DB or using GUI) or writing some kind of script that you will have to invoke whenever you are running db:migrate ( you will have to do it if you want to do proper testing). After awhile it becomes cumbersome and annoying.
- If you find that you are more and more annoyed with, to quote you "painfully wide tables and "type" columns" then:
- Your table design needs to be rethought and redone
- Your models may not be a good candidates for STI
- You just have to live with it.
Most IT problems really come down to this: Effort vs Benefit.
In your case you should ask yourself this question:
- How much time do you want to spend on implementing a better STI
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', '{**/**}')]
type
column would be good enough? From what I've seen with inheritance,SELECT *
would return all the associated columns (and I guess all unassociated columns too :/... But your DB structure would be cleaner.