Vita Rara: A Life Uncommon

Using Globally Unique Id's in Ruby on Rails


Categories:

I'm starting to play with Ruby on Rails a bit, and liking it so far. Over the past year I have been so preoccupied with work on ongoing projects that I really haven't had the time to experiment with RoR. I'll write more about why I finally got off the mark and tried it out later. Short story is I like to so far.

For years now I have moved away from using incrementing integers as id's in my database designs. I have been using string based globally unique id's. I really don't want to leave these behind, plus I'm looking at interoperating with some databases that use them already. So, I had to find a solution. It took a bit of Google'ing, but I think I've found a solution.

Take a look at the GUID plugin. I gave the code a look over. It seems very straight forward, and easy to integrate. I had to make some changes to my migrations, of which I'm still getting used to.

[UPDATE] If you are going to use this technique you will need to set

config.active_record.schema_format = :sql

in your environment.rb file. This will cause Rails to use the schema from your development database dumped in raw SQL to create your test database. Otherwise Rails will use its default of :integer as the id column type and your tests might fail in strange ways. I found this by testing the length of the id on a newly created entity, and it was 4 and the value was 0.