更新时间:2023-11-24 13:27:10
我认为你在这里把事情复杂化了.在我看来,您需要一个名为 memberships
的表,它应该充当用户、组织和角色之间的连接表
I think you're overcomplicating things here. The way I see it, you need a single table called memberships
that should act as a join table between User, Org and Role
class Membership
belongs_to :user
belongs_to :org
belongs_to :role
class User
has_many :memberships
has_many :orgs, through: :memberships
has_many :roles, through: :memberships
要为组织内的用户创建新角色,只需执行以下操作:
To create a new role for a user within an organization, just do:
org = Org.create({:fullname => 'Test Org 1'})
role = Role.where('name'=>'administrator').first
membership = user.memberships.create(org_id: org.id, role_id: role.id)
要查询组织内的角色,您可以:
To query for the roles within an organization, you can do:
org = Org.where(name: "Test Org").first
my_roles = Role.find(user.memberships.where(org_id: org.id).map(&:role_id))