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
.
Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database.
ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending. Edit: as Mike points out, in this case ActiveRecord is a module...
Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.
The active record pattern is an approach to accessing data in a database. A database table or view is wrapped into a class. Thus, an object instance is tied to a single row in the table. After creation of an object, a new row is added to the table upon save.
Rails 6.1 added a "native" way to implement Multiple Table Inheritance via delegated type
.
Please, see the corresponding PR for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With