Rails 4设计多个用户模型STI [英] Rails 4 Devise Multiple User Models STI

查看:180
本文介绍了Rails 4设计多个用户模型STI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的阅读。



我正在使用Devise和Rails 4.我想添加多个用户模型(admin,usertype1,usertype2),以便它们继承自主要用户模型我搜索了很多帖子,得出结论,我可以使用CanCan,我不想要,或者我可以使用单表继承。



我看到的方式是将类型字符串列添加到我使用Devise创建的主用户模型。我还需要从父级扩展每个子类,如:

  class Admin<用户
end

class Usertype1<用户
end

class Usertype2<用户
结束

我的问题是:下一步该怎么办?我如何知道如何访问类型列?我还需要覆盖 current_user 帮助器方法的Devise控制器,以便我可以使用 current_admin



再次感谢

解决方案

您还可以使用easy_roles宝石。它可以在github上使用。这个宝石为您的不同用户角色提供了一个bitmask解决方案。你只需要有一个模型,例如User,这个模型得到一个属性role。只需检查github上的文档。
Devise + easy_roles + CanCan是一个非常好的设置,在我看来非常方便。我经常使用这个。



链接到github: https: //github.com/platform45/easy_roles


Thanks for reading.

I am using Devise and Rails 4. I want to add multiple User Models(admin, usertype1, usertype2) such that they inherit from the main User Model. I have searched many posts and come to the conclusion that I may use CanCan, which I do not want, or I may use Single Table Inheritance.

The way I see it is to add a type string-column to my main User model I created with Devise. I will also need to extend each sub-class from the parent as in:

class Admin < User
end

class Usertype1 < User
end

class Usertype2 < User
end

My question is: what do I do next? How exactly do I know how to access the type column? Do I also need to override the Devise Controller for the current_user helper method such that I can have current_admin for example?

Thanks again.

解决方案

You can also use the easy_roles gem. It is available on github. This gem provides a bitmask solution for your different user roles. You just must have one model, e.g User, this model gets an attribute "role". Just checkout the docs on github. Devise + easy_roles + CanCan is a very good setup, it is very convenient in my opinion. I use this quite often.

Link to github: https://github.com/platform45/easy_roles

这篇关于Rails 4设计多个用户模型STI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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