2017-08-22 129 views
0

我有以下型号:Rails的Cancancan - 授权嵌套协会

class Party 
    belongs_to :user 
    has_many :party_co_hosts 
    accepts_nested_attributes_for :party_co_hosts 
    has_many :co_hosts, through: :party_co_hosts 

class CoHost 
    belongs_to :party 
    belongs_to :co_host, foreign_key: :user_id 

class User 

当事人应该属于用户(主机)和主机可以指定协办应该能够在编辑细节谁但不添加/删除共同主机。我正努力在Cancancan中定义这种能力。

can :update, Party, user_id: user.id 

这让主人可以做他们想要的派对 - 这很好。

can :update, Party do |p| 
    p.co_hosts.include? user 
end 

这将使协办编辑方,这也很好的能力,但我不希望这样的,包括嵌套party_co_hosts关联。

我是否应该删除accepts_nested_attributes_for并强制party_co_hosts通过单独的控制器创建,而不是在父级中允许嵌套关联?其他想法,我只是失踪?

回答

1

只是一个想法或许能有所帮助,我想新的设计为您的问题

class User < ApplicationRecord 
    has_many :roles 
    has_many :parties, through: :roles 
end 

class Role < ApplicationRecord 
    belongs_to :user 
    belongs_to :party 
end 

class Party < ApplicationRecord 
    has_many :roles 
    has_many :users, through: :roles 
end 

角色表中有场USER_ID,party_id和标题字符串,标题内容可以作为客人或合作-host,如果作为协办角色,则用户可以编辑方模型,如果作为嘉宾,他们只能读取等

,然后检查许可,您可以使用role base authorization