有人能给我一个如何使用现有数据库将设备添加到 2 个不同模型的示例吗?我有 2 个模型,客户和供应商。如果我只是在两个模型上添加 :confirmable 并进行迁移 rails g 迁移add_confirmable_to_devise
迁移数据库后,两个模型中都会包含可确认选项吗?
最佳答案
不,您必须创建两个单独的迁移:
rails g 迁移 add_devise_fields_to_customer
class AddDeviseFieldsToCustomer < ActiveRecord::Migration
def change
# Confirmable columns
add_column :customers, :confirmation_token, :string
add_column :customers, :confirmed_at, :datetime
add_column :customers, :confirmation_sent_at, :datetime
add_column :customers, :unconfirmed_email, :string
end
end
rails g 迁移 add_devise_fields_to_vendor
class AddDeviseFieldsToVendor < ActiveRecord::Migration
def change
# Confirmable columns
add_column :vendors, :confirmation_token, :string
add_column :vendors, :confirmed_at, :datetime
add_column :vendors, :confirmation_sent_at, :datetime
add_column :vendors, :unconfirmed_email, :string
end
end
这只是针对可确认的,因为这是您指定的模块。如果您需要其他设计模块(Trackable、DatabaseAuthenticatable 等),您也需要将这些列添加到迁移中。
您还必须将 :confirmable (以及您想要的任何其他功能)添加到模型本身。
关于ruby-on-rails - 添加设备:confirmable to 2 models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37111502/