如何设置典型用户 HABTM 角色关系
我对此很陌生,我正在使用 cancan + devise 进行用户身份验证。但是,我不太确定建立典型的用户 HABTM 角色关系意味着什么,也不太了解 HABTM 关系是什么。
谁能很好地解释它或给我指出一个好的教程或示例?
I'm quite new to this and I'm using cancan + devise for my user auth. However I'm not really sure what it means to set up a typical users HABTM roles relationship nor do I really understand what a HABTM relationship is.
Can anyone explain it really well or point me to a good tutorial or example?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HABTM 的意思是“拥有并属于许多”。您基本上需要一个表作为中间人来跟踪多个 id(称为直通表)。当作为典型的用户 HABTM 角色关系引用时,它们实际上意味着存在一个
User
模型、Role
模型、用户表、角色表和 Roles_users 表。不要忘记添加 HABTM -- Roles_users -- 表。典型的设置如下:然后您可以使用像普通说法
User.first.roles
和Role.first.users
那样的关联。还有情侣 Railscasts 解决您的问题。
HABTM means has and belongs to many. You basically need a table as a middle man to track multiple id's (called a through table). When referenced as a typical users HABTM roles relationship, they really mean there would be a
User
model,Role
model, users table, roles table, and a roles_users table. Don't forget to add the the HABTM -- roles_users -- table. A typical setup follows:You can then use the associations like normal saying
User.first.roles
andRole.first.users
.There are also a couple Railscasts on your issues.
Ruby on Rails 指南是一个很好的起点这里也本教程正是你想要的
The Ruby on Rails Guides are a good starting point here also this tutorial is exactly what you want