Rails 使用用户模型的单表继承 (STI)

发布于 2024-12-25 11:48:17 字数 692 浏览 2 评论 0原文

建立一个小型预订网站。有用户(登录并使用网站)和客人,他们被分配到房间用户可以是(是?)客人,但并非所有客人也是用户

我最初的想法是在 useruser 之间建立一个 belongs_to / has_one 关系。 guest 但也许 STI 可以在这里工作?作为用户&客人有first_namelast_nameemail等。设置模型是否有意义,例如user 和 guest 都继承自 person

我将推出自己的简单身份验证,因此 user 可能拥有的唯一附加字段是 password_digestroles_maskicon_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2025-01-01 11:48:17

正如建议的那样,最简单的方法是坚持性传播感染。例如,您可以设置单个 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 with CanCan 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 having admin 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文