Generating unique token on the fly with Rails

前端 未结 6 1475
醉话见心
醉话见心 2021-01-31 18:50

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

6条回答
  •  春和景丽
    2021-01-31 19:48

    Maybe you can do something using the actual time. Then you won't need to check if the token was already used by an user.

    new_token = Digest::MD5.hexdigest(Time.now.to_i.to_s + rand(999999999).to_s)
    user.user_info_token = new_token
    

提交回复
热议问题