使用Devise Rails的用户和管理员之间的唯一ID [英] Unique IDs between Users and Admins with Devise Rails

查看:124
本文介绍了使用Devise Rails的用户和管理员之间的唯一ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用本教程中的选项1安装了Devise并创建了用户和管理员 https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role



现在我需要一点帮助。当添加管理员时,它会创建一个不同的表,并且可能会出现管理员和普通用户可能拥有相同的ID。



那么我如何获取信息来自用户和管理员?例如我想显示所有用户?我必须遍历用户表和管理员表吗?



或者如果我正在显示一个帖子。我将如何知道要查找哪个表以获取用户或管理员信息?



我知道我可以添加一个角色列到用户表,但我想避免这一点。



我希望这有道理哈哈

解决方案

我强烈建议使用单表继承(STI)。您可以通过在用户中添加一个名为类型的列,使用字符串数据类型表,并创建一个管理员模型以及 normal_user 模型,两个模型将继承自 class NormalUser<用户
end

class Admin<用户
end

类型列是一个保留字,将保存一个值 NormalUser Admin 根据您创建的用户类型。



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


的属性

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 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

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

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

这篇关于使用Devise Rails的用户和管理员之间的唯一ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆