Generating a short UUID string using uuidtools In Rails
You don't need uuidtools for this. You can use Secure Random for this.
[1] pry(main)> require "securerandom"
=> true
[2] pry(main)> SecureRandom.hex(20)
=> "82db4d707c4c5db3ebfc349da09c991b7ca0faa1"
[3] pry(main)> SecureRandom.base64(20)
=> "CECjUqNvPBaq0o4OuPy8RvsEoCY="
Passing 4
and 5
to hex
will generate 8 and 10 character hex strings respectively.
[5] pry(main)> SecureRandom.hex(4)
=> "a937ec91"
[6] pry(main)> SecureRandom.hex(5)
=> "98605bb20a"
Rails - Saved uuid's length is shorter than 32 characters
It was the SQLite database that was causing the issue - moved to MySQL database and it works as expected.
I need to generate uuid for my rails application. What are the options(gems) I have?
There are plenty of options, I recommend not to add additional dependencies and use SecureRandom
which is builtin:
SecureRandom.uuid #=> "1ca71cd6-08c4-4855-9381-2f41aeffe59c"
See other possible formats here.
UUIDs in Rails3
Are you declaring another before_create method in your Image model? If so, you'll be overriding the one in the UUIDHelper module. You'll want to either declare the callback a different manner, or call super in the callback in your image model.
Edit: Maybe change the helper to look something like this:
module UUIDHelper
def self.included(base)
base.class_eval do
before_create :set_uuid
def set_uuid
self.uuid = UUID.timestamp_create.to_s
end
end
end
end
Convert a string representation of UUIDTools:UUID back to UUIDTools:UUID
Just use UUIDTools::UUID.parse
:
require "uuidtools"
uuid = UUIDTools::UUID.random_create
uuid.to_s
#=> "cd833ba3-97c5-4615-a2a0-a6c3e56b24b2"
UUIDTools::UUID.parse("cd833ba3-97c5-4615-a2a0-a6c3e56b24b2")
#=> <UUID:0x3fd33d0ac184 UUID:cd833ba3-97c5-4615-a2a0-a6c3e56b24b2>
UUIDTools::UUID.parse(uuid.to_s) == uuid
#=> true
broken create, save, update, and destroy on Join record when using Postgres UUIDs in Rails
There are pros and cons to retaining both id
and uuid
. For JSON APIs that expose uuid
, using Concerns
would be a Rails-ish implementation.
app/models/user.rb
class User < ActiveRecord::Base
include UserConcerns::Uuidable
end
app/models/concerns/user_concerns/uuidable.rb
module UserConcerns::Uuidable
extend ActiveSupport::Concern
included do
before_save :ensure_uuid!
end
def ensure_uuid!
self.uuid = generate_uuid if uuid.blank?
end
def generate_uuid
# your uuid using uuid_generate_v4() here
end
def to_param
uuid
end
end
Above implementation leaves out the uuid generation but I think above answer has a link to that.
Generating GUIDs in Ruby
As of Ruby 1.9, uuid generation is built-in. Use the SecureRandom.uuid
function.
For example:
require 'securerandom'
SecureRandom.uuid # => "96b0a57c-d9ae-453f-b56f-3b154eb10cda"
Same UUID being generated in multi-threaded application
You can synchronize uuid-generation method, and/or you can pregen uuid pool and generate more identificators in one thread when pool starts to deplete.
Related Topics
"Errno::Eaccess...Permission Denied" Running Compass Watch
Error Running 'Requirements_Osx_Brew_Libs_Install...' on MAC 10.7
Ruby on Rails: How to Check If a File Is an Image
Singleton Method VS. Class Method
What Are the Advantages of Mocha Over Rspec's Built in Mocking Framework
Deleting a Hash from Array of Hashes in Ruby
Can a Ruby Script Tell What Directory It's In
Do You Check in Your Rvmrc File
Ruby Array of Hash. Group_By and Modify in One Line
Time_Ago_In_Words and Localize
Netbeans 6.9.1 + Rails 3 + Ruby 1.9.2P0 Debugging
Subtract Dates in Ruby and Get the Difference in Minutes
Is It Acceptable Practice to Patch Ruby's Base Classes, Such as Fixnum
Ruby: How to Store and Display a Day of the Week