我有许多用户与多个帖子有不同的角色。这些角色是owner
,editor
,viewer
,none
。每个用户可能只有一个角色的职位。我曾经代表这是一个通过导轨关系有许多如下:Ruby on Rails中的帖子的用户和角色
class User < ActiveRecord::Base
has_many :roles
has_many :posts, :through => :roles
end
class Post < ActiveRecord::Base
has_many :roles
has_many :users, through => :roles
end
class Role < ActiveRecord::Base
attr_accessor :role
belongs_to :users
belongs_to :posts
end
凡角色属性用于指示用户对帖子里面类型的角色。 设置新角色时,我不能简单地使用<<
运算符,因为它不会设置role
属性。处理这种情况的首选方式是什么?我如何执行每个用户/帖子组合只有一个角色并在我的Role
创建逻辑中执行此操作?