I have to generate a unique and random string which is to be stored in database. For doing this I have used the "uuidtools" gem. Then in my controller I have added the following line:
require "uuidtools"
and then in my controllers create method I have declared a 'temp' variable and generating a unique and random 'uuid' string like this:
temp=UUIDTools::UUID.random_create
which is creating a string like this one:
f58b1019-77b0-4d44-a389-b402bb3e6d50
Now my problem is I have to make it short, preferably within 8-10 character. Now how do I do it?? Is it possible to pass any argument to make it a desirable length string??
Thanks in Advance...
You don't need uuidtools for this. You can use Secure Random for this.
Passing
4
and5
tohex
will generate 8 and 10 character hex strings respectively.Please see in detail, How I used securerandom in one of my project recently, definitely help you!
create usesguid.rb file in your lib/usesguid.rb and paste below code in that -
add following line in your config/application.rb to load file -
Create migration script for UUID function as mentioned below to -
Here is example for contact migration, how we can use it -
Final how to use into your model
This will help you to configure UUID for your rails application.
This can be useful for Rails 3.0, 3.1, 3.2 and 4.0 as well.
Please let me know If you have any issue while using it, so simple!