使用Devise Rails的用户和管理员之间的唯一ID [英] Unique IDs between Users and Admins with Devise Rails
问题描述
我已经使用本教程中的选项1安装了Devise并创建了用户和管理员 https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role
现在我需要一点帮助。当添加管理员时,它会创建一个不同的表,并且可能会出现管理员和普通用户可能拥有相同的ID。
那么我如何获取信息来自用户和管理员?例如我想显示所有用户?我必须遍历用户表和管理员表吗?
或者如果我正在显示一个帖子。我将如何知道要查找哪个表以获取用户或管理员信息?
我知道我可以添加一个角色列到用户表,但我想避免这一点。
我希望这有道理哈哈
我强烈建议使用单表继承(STI)。您可以通过在 类型列是一个保留字,将保存一个值 要创建管理员使用 I have installed Devise and created Users and also Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role Now I need a little help. When adding the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's. So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table? Or if I am displaying a post. How will I know which table to look for to get the user or admin info? I know I can just add a role column to the users table but I wanted to avoid this. I hope this makes sense haha I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named The type column is a reserved word which will hold a value either To create an admin use 这篇关于使用Devise Rails的用户和管理员之间的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!用户中添加一个名为
表,并创建一个类型
的列,使用字符串
数据类型管理员
模型以及 normal_user
模型,两个模型将继承自
class NormalUser<用户
end
class Admin<用户
end
NormalUser
或 Admin
根据您创建的用户类型。
Admin.create(...)
并创建一个普通用户使用 NormalUser.create(...)
其中的点是 Admin
和 NormalUser
的属性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
NormalUser
or Admin
according to the user type you created.Admin.create(...)
and to create a normal user use NormalUser.create(...)
where the dots are the attributes of the Admin
and the NormalUser