且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Devise Rails 的用户和管理员之间的唯一 ID

更新时间:2023-11-04 15:56:46

我强烈建议使用单表继承 (STI).您可以通过在 users 表中添加名为 type 的列和 string 数据类型并创建一个 admin 来执行 STI> 模型以及 normal_user 模型,这两个模型都将从 devise gem 的 user 模型继承.

I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.

class NormalUser < User
end

class Admin < User
end

类型列是一个保留字,它将根据您创建的用户类型保存一个 NormalUserAdmin 值.

The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.

要创建管理员使用 Admin.create(...) 并创建普通用户使用 NormalUser.create(...) 其中点是AdminNormalUser

To create an admin use Admin.create(...) and to create a normal user use NormalUser.create(...) where the dots are the attributes of the Admin and the NormalUser