为rails中的16位二进制数据类型生成uuid
我用过这个'SecureRandom.uuid‘,但它需要很大的尺寸。它占用很大的空间,我有binary(16)数据类型和大小,database=mysql;uuid = SecureRandom.uuid
我想要16个大小的uuid和存储在二进制数据类型。
发布于 2019-07-12 15:17:37
试试这个:
require 'securerandom'
uuid = SecureRandom.hex 16在一个典型的模型中,你会这样做:
before_create :set_uuid
def set_uuid
self.uuid ||= SecureRandom.hex 16 # or self.id, whatever your attribute is named
end如果您使用的是PG,那么它对UUID的支持很好,而对性能的影响很小,所以您可以在迁移中只定义uuid列。https://lab.io/articles/2017/04/13/uuids-rails-5-1/
https://stackoverflow.com/questions/57001731
复制相似问题