简体   繁体   English

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

[英]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_touserguest之间建立一个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_namelast_nameemail等,是否有必要设置模型以使userguest都继承自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_digestroles_maskicon_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.

相关问题 Rails:这是单表继承(STI)的用例吗? - Rails: Is this a use case for Single Table Inheritance (STI)? 替代Rails单表继承(STI)? - Alternative to Rails Single Table Inheritance (STI)? 表单中的Rails单表继承(STI) - Rails Single Table Inheritance (STI) in a form Ruby on Rails单表继承(STI)和单元测试问题(使用PostgreSQL) - Ruby on Rails Single Table Inheritance (STI) and unit test problem (with PostgreSQL) Rails单表继承(STI):对派生模型的依赖 - Rails Single-Table Inheritance (STI): dependencies on the derived models 使用单表继承(STI)创建子类 - Creating subclasses using Single table Inheritance (STI) 单表继承(STI)列关联 - Single Table Inheritance (STI) column associations 如何使用单表继承(STI)在Ruby on Rails中为继承的资源生成适当的路由助手 - How do I generate appropriate route helpers for an inherited resource in Ruby on Rails using single table inheritance (STI) 无效的单表继承类型:尝试在Rails 4中设置STI子类时出错 - Invalid single-table inheritance type: Error when TRYING to set a STI subclass in Rails 4 我应该如何在Rails 4.0中使用单表继承(STI)来处理“通过”关系 - How should I approach “has many through” relationships with Single Table Inheritance (STI) in Rails 4.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM