Saturday, 31 August 2013

Generating unique token on the fly with Rails

Generating unique token on the fly with Rails

I want to generate a token in my controller for a user in the
"user_info_token" column. However, I want to check that no user currently
has that token. Would this code suffice?
begin
@new_token = SecureRandom.urlsafe_base64
user = User.find_by_user_info_token(@new_token)
end while user != nil
@seller.user_info_token = @new_token
Or is there a much cleaner way to do this?

No comments:

Post a Comment