Use a random string as id in Ruby on Rails?

后端 未结 4 713
暖寄归人
暖寄归人 2021-02-06 18:45

I want to create a web app similar to http://www.pastebin.com/ in Ruby on Rails. pastebin.com uses a random string to identify an item. Ruby on Rails uses an auto-incrementing n

4条回答
  •  攒了一身酷
    2021-02-06 19:04

    generate a random string as key and put it into a db table? make sure the key is uniq?

    base="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    (0...10).map{base[rand(base.length)]}.join
    

提交回复
热议问题