← Back to Upcase

UUID in Rails 4


(Charlie Gaines) #1

Any opinions on when and where it is useful to use the new UUID support in Rails 4?


(Derek Prior) #2

UUIDs are useful when you need non-sequential identifiers for any reason. For instance, if you have an application that has multiple replicating databases, then using a typical sequenced integer id would be problematic as those integers would be duplicated across databases.

Additionally, you may find yourself in a situation where you are uncomfortable exposing integer ids because they reveal more about your system or a customers data than you would like (how many of a certain record type do you have? How many were created today? What happens s if I tweak this id in this form post?). In these situations, UUIDs are very helpful.


(Geoff Harcourt) #3

@charliegaines, in a second positive benefit to what Derek mentioned, UUID is also great when you’re launching an application and don’t want your users to know they are only the 44th person to become a member when you need the illusion of a larger user base.


(Derek Prior) #4

If that’s your only worry, you could start the sequence off at 100000 or so!


(Geoff Harcourt) #5

Clearly your level of paranoia is in the realm of normality, @derekprior.


(Charlie Gaines) #6

Thanks for the replies. Any downsides you’ve encountered?


(Geoff Harcourt) #7

Tables with UUIDs require a little extra attention whenever they are referenced by foreign key on other tables. The default migration generator won’t make the FK columns UUIDs, it instead automatically makes them integers unless you modify the migration.

See a blog post about this here:
http://labria.github.io/2013/04/28/rails-4-postgres-uuid-pk-guide/