← Back to Upcase

What are best practices to generate object ids for rails api?

(Michał Młoźniak) #1

I am building api in rails and I am wondering what is the best way to implement object ids. I don’t want to use database generated id.

This article http://railsware.com/blog/2013/04/08/api-with-ruby-on-rails-useful-tricks/ suggests using guid, but I don’t like the format :slight_smile: I would prefer ids similar to parse api. Are there any gems that do this or should I just use securerandom and catch duplicates?

Can anyone point to some good articles or share some tips, best practices about implementing api in rails?

(Geoff Harcourt) #2

Format aside, a modern UUID saves you the practical trouble of duplicates:

Rails 4 features (decent) support for (type IV) UUID as primary key, so you can just skip to that and avoid integers completely on that table if it’s easier.

(Chad Pytel) #3

@ronin can you clarify why you don’t want to use the database generated id?

(Michał Młoźniak) #4

Mostly because of the enumeration problem, that some can guess number of records just by looking at id values. Or guess ids of objects from other users and so on.

(Chad Pytel) #5

I don’t necessarilly agree with the priority of your concerns, however if you are then I do recommend you go with the uuid.

It’s been a while, what did you end up going with?