[英]Rails Single table inheritance (STI) with the User model
Building a small reservation site. 建立一个小的预订网站。 There are users
(who login and work with the site) and there are guests
who are being assigned to rooms
. 有users
(登录并使用该站点的用户),并且有被分配到rooms
guests
。 users
can be (are?) guests
but not all guests
are also users
. users
可以是(是?) guests
但并非所有guests
也是users
。
My initial inclination was to set up a belongs_to
/ has_one
relationship between user
& guest
but maybe STI would work here? 我最初的belongs_to
在user
和guest
之间建立一个belongs_to
/ has_one
关系,但STI可能在这里起作用? Being as users & guests have first_name
, last_name
, email
etc. does it make sense to set up the model such that, say, user
and guest
both inherit from person
? 作为用户和来宾拥有first_name
, last_name
, email
等,是否有必要设置模型以使user
和guest
都继承自person
?
I will roll my own simplistic authentication so the only additional fields user
is likely to have are password_digest
, roles_mask
and a icon_color
. 我将进行自己的简单身份验证,因此user
可能仅有的其他附加字段是password_digest
, roles_mask
和icon_color
。
Any suggestions? 有什么建议么? I only ask because things can get tricky around authentication, authorization & whatnot. 我只是问,因为在身份验证,授权和其他方面可能会变得棘手。
Appreciate any ideas/tips! 感谢任何想法/提示!
The simplest approach here would be to, as suggested, stick to STI. 如建议的那样,这里最简单的方法是坚持STI。 You can, for example, setup a single devise
User
model as well as apply ACL with CanCan
and define roles for your users. 例如,您可以设置单个devise
User
模型,并在CanCan
应用ACL并为用户定义角色。
CanCan
's ability spec will determine which resources are accessible and what are not. CanCan
的能力规格将确定哪些资源可访问,哪些资源不可访问。 The advantage here is that users can be guests, and depending on how you setup your ACL, guests can be prevented from having admin
like access. 这样做的好处是用户可以成为访客,并且根据您设置ACL的方式,可以防止访客具有类似admin
访问权限。
However, Jesse's suggestion of going two separate Devise models is also a good idea as this ensures their sessions are separate. 但是,Jesse建议使用两个单独的Devise模型也是一个好主意,因为这可以确保它们的会话是分开的。 This is more straightforward to implement as you can then setup a User-specific ACL and Guest-specific ACL accordingly. 实施起来更直接,因为您可以相应地设置特定于用户的ACL和来宾特定的ACL。
https://github.com/ryanb/cancan https://github.com/ryanb/cancan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.