What are the best practices to working with external IDs in REST API in general, and specifically in Rails?
Some Background:
I created an API for my application, which uses standard resource based JSON REST API.
I have a Flight model, by which users to create model objects by POSTing to /api/flights. This assigns a unique id to the new Flight objects the way ActiveRecord always does.
So far pretty standard.
However, my customer wants me to provide an option to assign an id by himself as part of the creation of the Flight object in the API, and later use the id he assigned in order to show/update/delete (etc.) the object by API.
I figure I can simply add an external_id parameter and add routes to update/delete/index by external_id. However, this seems to clutter the elegant resource based rails approach.
So, what’s the best practice?
I think this is fine. Another similar case is creating a “slug” or “friendly url” for a URL which replaces the less friendly ID (“augments” is a better word than replaces). As long as the
external_idis immutable, unique within its scope, URL-safe and so on it’s equivalent to the ID. Because it’s an external value, however, you need to confirm with your client that it is immutable; while possible to have it change, there’s a lot of work needed to handle changing the value (especially if it’s part of a URL).If you want to replace Rails’ find method to use the external_id instead, use
method_missingto redefine id — there are examples of this around by googling “rails 3 slug method_missing” and similar.