好像默认的:id是整数,我可以把它做成字符串吗?
最佳答案
是的,你可以。
首先运行迁移:
create_table 'table' id: false, force: true do |t|
t.string 'id', null: false
end
为 id 指定类型 string
。
然后在你的模型中:
class Table < ActiveRecord::Base
self.primary_key = :id
end
这将基本上明确指示字符串 id
是对象实例的主键。
关于ruby-on-rails - 我可以在使用 Active Record 时将 ID 更改为字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33702103/