Home Generate unique random token in ruby on rails
Post
Cancel

Generate unique random token in ruby on rails

Introduction

Generating unique random token for ruby on rails application is a common problem. People use different ways to solve this problem and the most common way to solve this problem that we can find in the internet is to generate a SecureRandom.hex like this:

1
2
3
4
5
6
7
def generate_token
  loop do
    token = SecureRandom.hex(32)

    break token unless Record.where(token: token).exists?
  end
end

Here we are telling this method to generate a random token using SecureRandom.hex(), and than do a ActiveRecord query on a Record model to see if that token already exists in the database. If it does, then generate another token, until the unique none existing token is found. you can read more about this method from SecureRandom.

I think we can improve this method to generate more unique token by adding Unix Timestamp in combination with SecureRandom.hex() token like this:

1
2
3
4
5
6
7
8
9
def generate_token
  loop do
    token = SecureRandom.hex(20)
    unix_timestamp = Time.current.to_time.to_i # e.g output => 1672645137
    token += unix_timestamp.to_s

    break token unless Record.where(token: token).exists?
  end
end

I think combining unix timestamp with SecureRandom hex can give more unique token, hence less repetition to match the unique token in database.

This post is licensed under CC BY 4.0 by the author.