Why doesn't ActiveRecord in Rails support multiple table inheritance?

I tried to implement a set of models that I put together on paper, and ran into a problem when, as I thought, the best way to do this would be to use multiple inheritance tables. However, after searching on Google, I found that ActiveRecord does not support MTI ... although there are many articles showing how to do this. This made me wonder if I configured my models correctly if it is not implemented. So my question is: why does Active Record not support MTI? If you are interested in how my model was โ€œsimilarโ€, I will leave it below.

class Player < ActiveRecord::Base; end class CollegePlayer < Player; end class ProPlayer < Player; end 

If a player can be one or both colleges and ProPlayer. Or in another example ...

 class Person < ActiveRecord::Base; end class User < Person; end class Player < Person; end class Coach < Person; end 

If "Face" may be User , former Player and / or Coach .

+6
source share
1 answer

This short answer to your question is that the core ActiveRecord team does not want to include this feature in conjunction with insufficient community demand. see https://github.com/rails/rails/issues/5541

If you want a definitive answer, you will need to ask DHH (david.heinemeierhansson.com) or Aaron Tender Love Patterson ( tenderlove@github.com ), as they are the "leading" participants in the active recording project.

My personal answer is that active recording is already quite complicated. The number of metaprograms that run for ActiveRecord to support the features it supports is already a little difficult to maintain and extend. The core team that is currently working on AR pays great attention to further optimizing AR to work faster and reorganize it to clean the organization. Given that there are already alternatives to getting multiple tables, such as inheritance, and the community does not require this feature to be less prioritized.

While commenting on your other question about the correctness of your DB organization, I agree with muistooshort, it looks like you can accomplish what you want with roles. When I first came up with C ++, I tried to make separate models for different objects and felt a strong need to stick to some kind of intricate inheritance scheme. You just need to ask yourself, since your application is now (and not down the road, because it is speculation at this stage) - this is a difference in behavior, so different between these types that it justifies this degree of differentiation.

EDIT

I spoke with Sean Griffin from Thoughtbot, who is working on Active Record, and I asked him why multiple table inheritance does not support his answer:

"Polymorphic associations fill this role for most use cases."

+1
source

Source: https://habr.com/ru/post/980723/


All Articles