Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ActiveRecord in Rails not support Multiple Table Inheritance?

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.

like image 373
daveomcd Avatar asked Jan 08 '15 19:01

daveomcd


People also ask

What is ActiveRecord in Ruby on Rails?

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.

What does ActiveRecord base do?

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...

Is ActiveRecord part of rails?

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.

What are ActiveRecord methods?

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.


1 Answers

Rails 6.1+ delegated type

Rails 6.1 added a "native" way to implement Multiple Table Inheritance via delegated type.

Please, see the corresponding PR for details.

like image 66
Marian13 Avatar answered Oct 21 '22 03:10

Marian13