带有 sqlite3 的 rails5 中 id 模型的 lambda uuid 生成器
lambda uuid generator for id's model in rails5 with sqlite3
由于sqlite3中没有uuid处理,我尝试自己生成。
许多其他帖子让我想到了这个:
create_table :users, id: false do |t|
t.string :id, :primary_key => true, null: false, default: -> { "(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))))" }
...
end
当您执行 User.create ...
.
时,它实际上会在 id
字段中生成默认的随机 uuid
但是当你做 User.new
时,这实际上是我在控制器中的情况(通过默认生成的视图)我推送 New User
link 我有:
- 在我的表格中
id
值:new_user_lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))
- 即使我将表单选项更正为
form_for @user, html: {id: "new_user"}
,当我提交表单时,我也会被重定向到 http://localhost:3000/accounts/lower(hex(randomblob(4)))%20%7C%7C%20'-'%20%7C%7C%20lower(hex(randomblob(2)))%20%7C%7C%20'-4'%20%7C%7C%20substr(lower(hex(randomblob(2))),2)%20%7C%7C%20'-'%20%7C%7C%20substr('89ab',abs(random())%20%25%204%20+%201,%201)%20%7C%7C%20substr(lower(hex(randomblob(2))),2)%20%7C%7C%20'-'%20%7C%7C%20lower(hex(randomblob(6)))
。
这似乎也是通过 User.new
. 字段 id
中的值
所以我想知道是否有一种方法可以在通过 new
创建 id
字段时生成默认值,例如 SecureRandom
或类似的东西.
呃,解决方法其实很简单:
在我的 app/models/application_record.rb
:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
after_initialize :generate_uuid
protected
def generate_uuid
self.id = SecureRandom.uuid unless self.id
end
end
可能遇到同样情况的人:
- 我显然没有保留
id
字段 (t.string :id, default: -> { "(lower..."}
) 的默认值定义。
- 我把
id
默认集合的类型改成了:string
(create_table :users, id: :string do |t|
)。
由于sqlite3中没有uuid处理,我尝试自己生成。
许多其他帖子让我想到了这个:
create_table :users, id: false do |t|
t.string :id, :primary_key => true, null: false, default: -> { "(lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6))))" }
...
end
当您执行 User.create ...
.
时,它实际上会在 id
字段中生成默认的随机 uuid
但是当你做 User.new
时,这实际上是我在控制器中的情况(通过默认生成的视图)我推送 New User
link 我有:
- 在我的表格中
id
值:new_user_lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))
- 即使我将表单选项更正为
form_for @user, html: {id: "new_user"}
,当我提交表单时,我也会被重定向到http://localhost:3000/accounts/lower(hex(randomblob(4)))%20%7C%7C%20'-'%20%7C%7C%20lower(hex(randomblob(2)))%20%7C%7C%20'-4'%20%7C%7C%20substr(lower(hex(randomblob(2))),2)%20%7C%7C%20'-'%20%7C%7C%20substr('89ab',abs(random())%20%25%204%20+%201,%201)%20%7C%7C%20substr(lower(hex(randomblob(2))),2)%20%7C%7C%20'-'%20%7C%7C%20lower(hex(randomblob(6)))
。
这似乎也是通过User.new
. 字段
id
中的值
所以我想知道是否有一种方法可以在通过 new
创建 id
字段时生成默认值,例如 SecureRandom
或类似的东西.
呃,解决方法其实很简单:
在我的 app/models/application_record.rb
:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
after_initialize :generate_uuid
protected
def generate_uuid
self.id = SecureRandom.uuid unless self.id
end
end
可能遇到同样情况的人:
- 我显然没有保留
id
字段 (t.string :id, default: -> { "(lower..."}
) 的默认值定义。 - 我把
id
默认集合的类型改成了:string
(create_table :users, id: :string do |t|
)。