Rails 使用 CanCan gem 进行授权时,模型/数据库应该是什么样的?
我正在使用设备作为授权。并且想知道模型和数据库应该如何。在此 ailscasts 剧集中 http://railscasts.com/episodes/192-authorization-with-cancan Ryan 说他在角色和用户之间使用多对多。
我的用户模型:'
class User < ActiveRecord::Base
has_and_belongs_to :roles
has_many :posts
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
我的角色模型:
class Role < ActiveRecord::Base
has_and_belongs_to :users
end
但是角色表应该是什么样的?
我正在考虑创建角色表,例如:
id
admin - Boolean
moderator - Boolean
author - Boolean
然后创建表 Roles_users:
id
role_id
user_id
但是为什么一个用户应该拥有多个角色?当有布尔值时?
I am using devise as authorization. And are wondering how the model and database should be. In this ailscasts episode http://railscasts.com/episodes/192-authorization-with-cancan says Ryan that he is using a many to many between roles and users.
My User model:'
class User < ActiveRecord::Base
has_and_belongs_to :roles
has_many :posts
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
My Role model:
class Role < ActiveRecord::Base
has_and_belongs_to :users
end
But how should the roles table be like?
I was thinking creating the role table like:
id
admin - Boolean
moderator - Boolean
author - Boolean
And then the table roles_users:
id
role_id
user_id
But why should a user have many roles? When having booleans?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
角色表就是:
这样您就可以在不更改架构的情况下添加角色。
The roles table would just be:
That way you can add roles without changing schema.