如何建立典型的用户

如何建立典型的用户

本文介绍了如何建立典型的用户 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?

推荐答案

HABTM 意味着拥有并属于许多.您基本上需要一个表作为中间人来跟踪多个 ID(称为直通表).当被引用为典型的用户 HABTM 角色关系时,它们实际上意味着将有一个 User 模型、Role 模型、users 表、roles 表和一个 roles_users 表.不要忘记添加 HABTM --roles_users -- 表.典型设置如下:

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:

class User < ActiveRecord::Base
  has_and_belongs_to_many :roles
end

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
end

然后您可以像平常说的User.first.rolesRole.first.users 一样使用关联.

You can then use the associations like normal saying User.first.roles and Role.first.users.

还有一对夫妇 Railscasts 关于您的问题.

There are also a couple Railscasts on your issues.

这篇关于如何建立典型的用户 HABTM 角色关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 22:30