Rails 使用用户模型的单表继承 (STI)
建立一个小型预订网站。有用户
(登录并使用网站)和客人
,他们被分配到房间
。 用户
可以是(是?)客人
,但并非所有客人
也是用户
。
我最初的想法是在 user
和 user
之间建立一个 belongs_to
/ has_one
关系。 guest
但也许 STI 可以在这里工作?作为用户&客人有first_name
、last_name
、email
等。设置模型是否有意义,例如user 和
guest
都继承自 person
?
我将推出自己的简单身份验证,因此 user
可能拥有的唯一附加字段是 password_digest
、roles_mask
和 icon_color
代码>.
有什么建议吗?我之所以这么问,是因为身份验证、授权和验证方面的事情可能会变得棘手。诸如此类的。
感谢任何想法/提示!
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
can be (are?) guests
but not all guests
are also users
.
My initial inclination was to set up a belongs_to
/ has_one
relationship between user
& guest
but maybe STI would work here? 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
?
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
.
Any suggestions? I only ask because things can get tricky around authentication, authorization & whatnot.
Appreciate any ideas/tips!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如建议的那样,最简单的方法是坚持性传播感染。例如,您可以设置单个
devise
User
模型,以及通过CanCan
应用 ACL 并为您的用户定义角色。CanCan
的能力规范将确定哪些资源可以访问,哪些资源不可访问。这样做的优点是用户可以是访客,并且根据您设置 ACL 的方式,可以阻止访客拥有类似于管理员的访问权限。然而,Jesse 建议使用两个单独的 Devise 模型也是一个好主意,因为这可以确保它们的会话是分开的。这更容易实现,因为您可以相应地设置用户特定的 ACL 和访客特定的 ACL。
https://github.com/ryanb/cancan
The simplest approach here would be to, as suggested, stick to STI. You can, for example, setup a single
devise
User
model as well as apply ACL withCanCan
and define roles for your users.CanCan
's ability spec will determine which resources are accessible and what are not. The advantage here is that users can be guests, and depending on how you setup your ACL, guests can be prevented from havingadmin
like access.However, Jesse's suggestion of going two separate Devise models is also a good idea as this ensures their sessions are separate. This is more straightforward to implement as you can then setup a User-specific ACL and Guest-specific ACL accordingly.
https://github.com/ryanb/cancan