17

Are there standards or best practices yet when it comes to multi table inheritance in rails 3? So far the best article I could find was:

http://mediumexposure.com/multiple-table-inheritance-active-record/

But even that needed some changes(e.g. moving the requires to an initializer instead of the old /config/environment.rb)

Any better resources / standards?

jtesch
  • 778
  • 1
  • 7
  • 9

3 Answers3

14

For an easy way to do multi-table inheritance take a look at 'acts_as_relation' plugin https://github.com/hzamani/acts_as_relation.

Community
  • 1
  • 1
Hassan
  • 151
  • 1
  • 2
  • Beautiful & readable API, simple to use. This would have saved me a ***LOT*** of problems several years ago before any good MTI solution existed for Rails. – iconoclast Sep 03 '13 at 18:45
  • Nice plugin. It's worth mentioning however that it doesn't seem to maintain referential integrity at the data layer. If that is important to you, Dan Chak covers this nicely in the (free) online version of [Enterprise Rails](https://dan.chak.org/enterprise-rails/chapter-10-multiple-table-inheritance) – doub1ejack Sep 27 '16 at 11:51
  • @Hassan, but in the case of `acts_as-relation` gem with devise gem, where do I place my SignUp Form? My `Clients` devise model is actable, and 3 other models `acts_as :clients`. How do I deal with my SignUp form? Those other 3 models acting as clients has different content for siging up. – Afolabi Olaoluwa Jan 11 '17 at 07:44
6

I recently forked a promising gem to implement multiple table inheritance and class inheritance in Rails. I have spent a few days subjecting it to rapid development, fixes, commenting and documentation and have re-released it as CITIER (Class Inheritance and Table Inheritance Embeddings for Rails).

Consider giving it a look: https://github.com/PeterHamilton/citier

It actually takes some concepts from that article you mention.

Jason Gilmore
  • 3,698
  • 3
  • 23
  • 28
Pete Hamilton
  • 7,730
  • 6
  • 33
  • 58
  • Just checking out your example on your page, and when you call `d.save` to save your Dictionary object, it looks like it's calling 3 updates, the first 2 of which are overwritten: 1. `citier -> UPDATE products SET type = 'Product' WHERE id = 1` 2. `citier -> SQL : UPDATE products SET type = 'Book' WHERE id = 1` 3. `citier -> SQL : UPDATE products SET type = 'Dictionary' WHERE id = 1` Is this accurate? Or are the first 2 ignored and only the last one is called? – Matt Huggins Jun 16 '11 at 00:37
2

There's a guy in the Melbourne Ruby group I attend that's written a couple of blogs on table inheritance in rails and the comments are really helpful as well. It's not specifically Rails 3 but there's definitely some decent pointers in there.

http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading

http://rhnh.net/2010/07/02/3-reasons-why-you-should-not-use-single-table-inheritance

happy reading

2potatocakes
  • 2,240
  • 15
  • 20