Rails polymorphic association without the type column, possible?

These last two weeks has been very hard on me, this post will describe why and how you can help me

Around two weeks ago, I started doing heavy database optimizations on the Gogobot website, after a discussion with a DBA from Percona.

Changing the database schema of such a big app is not easy let me tell you, you need to make sure you break nothing, write more tests, unit and integration.

I found out many things about Rails, ActiveRecord and mysql during those 2 weeks, I found creative ways to save many GB on the tables size and on the index size. (posts coming up, I promise).

Now, one of the things (very first things) the DBA mentioned was that the use of polymorphic associations is not recommended in mysql.

Since when you use it, you need two columns item_id, item_type (in most cases).

item_type is a string “Hotel”, “User” etc… and since this is UTF-8 it’s actually taking up lots of space on the row size and on the index size.

Because of mysql limitations, when your row is too big, mysql will fall back to disk, and this is a very bad thing (VERY!)

What he suggested was to move to an enum column in mysql and just have all of our type in the enum, this way you can save lots of time on the query (like 50%) and the index will be much smaller, which will also result in querying straight from the index (if you use the right queries).

To make a long story short.

Rails doesn’t support mysql enum column (and boy did it take energy to find out) and for some reason even using the enum_column_3 gem had errors when dumping the schema to the schema.rb file.

Some air, thinking time…

At Gogobot, we have unique id’s for each object, no model has the same range of id’s as another model, from the id I can have the model type, I don’t need the class name.

We made it so we can simplify API calls and not use a composite id.

Now, we have that, so I was wondering if I could leverage that into using it in the polymorphic associations.

I started going through the AR code, but this is an open call for all you Rails experts out there, can you suggest a solution for this situation?


I would love a discussion on that as well…


<img src="http://feeds.feedburner.com/~r/KensoDev-en/~4/RYuJBP1Desg" height="1" width="1"/>