0

Newbie here, wrapping my head 'round this stuff!

I'd like to use the hex number as my url (external identifier) and keep the uuid within the database for a ruby on rails application. Is this even possible?

Thanks a bunch

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
asaignment
  • 941
  • 2
  • 8
  • 12

1 Answers1

1

Many people advise you against it but, yes, it is possible. It will need some code for it, and the solution depends on which version of Rails you use and what you use for the database, which is why I'm going to answer in a generic way.

You will want to have two different fields for the model: one for the external hex representation and another one for a separate UUID. Then, you can use the hex string to find instances in your controller actions, for example.

Please take a look at the following (they don't seem to have the two fields but will point you to the right direction anyway):

Problems setting a custom primary key in a Rails 4 migration

Change Primary Key Issue Rails 4.0

http://www.speakingcode.com/2013/12/07/gracefully-using-custom-primary-keys-in-rails-4-routes-controllers-models-associations-and-migrations/

And a longer post of a similar thing to do: http://ruby-journal.com/how-to-override-default-primary-key-id-in-rails/

Also, the FriendlyId gem might do what you want.

Community
  • 1
  • 1
Jarno Lamberg
  • 1,530
  • 12
  • 12
  • Thank you - this is very helpful! Why is it advised against? – asaignment Nov 01 '14 at 09:46
  • Because a lot of Ruby on Rails "magic" relies on having an integer field for the primary key, for example, and without one it is harder to make sure everything works as expected. – Jarno Lamberg Nov 01 '14 at 22:14