Reputation: 6565
I was trying to implement a set of models I had put together on paper and ran into an issue where I thought the best way to go about it would be using a Multiple Table Inheritance setup. However, after google searching I found that ActiveRecord doesn't support MTI... even though there are plenty of articles showing how it can be done. This had me wondering if I was setting up my models correctly if it's not implemented. So my question is why doesn't Active Record have built in support for MTI? If you are curious to what my model setup was 'going' to look like I'll leave it below.
class Player < ActiveRecord::Base; end
class CollegePlayer < Player; end
class ProPlayer < Player; end
Where a Player can be either or both of CollegePlayer and ProPlayer. Or in another example...
class Person < ActiveRecord::Base; end
class User < Person; end
class Player < Person; end
class Coach < Person; end
Where a "Person" could be a User
, former Player
, and/or Coach
.
Upvotes: 7
Views: 2240
Reputation: 9328
Rails 6.1 added a "native" way to implement Multiple Table Inheritance via delegated type
.
Please, see the corresponding PR for details.
Upvotes: 2
Reputation: 1885
This short answer to your question is because the ActiveRecord core team does not want to include this feature coupled with not enough demand from the community. see https://github.com/rails/rails/issues/5541
If you want the definitive answer you would have to ask DHH (david.heinemeierhansson.com) or Aaron Tender Love Patterson ([email protected]) as they are the "leading" contributors on the active record project.
My personal answer is because active record is already complex enough. The amount of meta programming that is done for ActiveRecord to support the features it already support is already a bit challenging to maintain and extend. The core team that is currently working on AR have been putting a great deal of focus into further optimizing AR to work faster and refactoring it to clean up the organization. Given that there are already alternatives to getting multiple table like inheritance and the community isn't clamoring for this feature its less of a priority.
Though to comment on your other question about the correctness of you DB organization I agree with muistooshort it seems like you can accomplish what you want with roles. When I was first coming from C++ I tried to make separate models for different objects and felt a strong need to adhere to some sort of convoluted inheritance scheme too. You just need to ask yourself as you app stands now (not down the road because thats speculation at this point) is the difference in behavior so different between these types that it justifies this degree of differentiation.
EDIT
I spoke with Sean Griffin of Thoughtbot who works on the Active Record and I asked him why multiple table inheritance was not support his response was:
"Polymorphic associations fill that role for most use cases"
Upvotes: 1