hzamani / active_record-acts_as

Simulate multi-table inheritance for activerecord models
MIT License
252 stars 86 forks source link

Same ID inheritance #77

Closed ludalex closed 7 years ago

ludalex commented 8 years ago

I found myself to work with a legacy schema where table inheritance is done by referencing the two tables with the "id" column, where the inheriting tables have "id" which is a foreign key to the inherited "id". No "actable_id" and "actable_type" is just "type":

+------------+  +---------+  +---------+  +---------+
|location    |  |bar      |  |pub     |   |park     |
+------------|  +---------+  +---------+  +---------+
|id          |  |id       |  |id       |  |id       |
|type        |  |title    |  |title    |  |title    |
+------------+  |open     |  |waiters  |  |people   |
                +---------+  +---------+  +---------+

This kind of class table inheritance is exactly the same used by the sequel plugin to do that, which in fact works great with my schema... but I would really like to use AR instead. Main reason is to use it in combination with jsonapi-resources.

I was wondering if this was easily doable to offer as an option or it would completely break your design.

pablodgcatulo commented 8 years ago

That's how Martin fowler defined it. Bill Karwin sugest that model also. The polymorphic design is not so bad, but mix data with metadata. If we were using the same id on the child tables as in the parent table, will allow us to create referential integrity over all relations.

ludalex commented 7 years ago

Unfortunately I'm not in a position to work on a fork to accomplish this - but I would really need it for a project that involves working on existing a schema structured with inheritance as described above.

Would it be trivial to add this as an option @hzamani? I think many people will benefit.

manuelmeurer commented 7 years ago

I think this will be quite a major change but it should be doable in theory in a fork.

manuelmeurer commented 7 years ago

Closing this for now, if anyone is interested to work on this please fork https://github.com/krautcomputing/active_record-acts_as