Ruby on rails Rails Cancancan-授权嵌套关联
我有以下型号:Ruby on rails Rails Cancancan-授权嵌套关联,ruby-on-rails,cancan,cancancan,Ruby On Rails,Cancan,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 Use
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
参与方应属于用户(主机),主机可以分配能够编辑参与方详细信息但不能添加/删除共同主机的共同主机。我正在坎坎坎努力定义这种能力
can :update, Party, user_id: user.id
这让主人全权为聚会做他们想做的事——这很好
can :update, Party do |p|
p.co_hosts.include? user
end
这将使共同主持能够编辑聚会,这也是很好的,但我不希望它包括嵌套的聚会共同主持
关联
我是否应该删除的
接受嵌套的属性,\u并强制通过单独的控制器创建参与方的共同主机
,而不是允许父级中的嵌套关联?我还缺少其他想法吗?一个想法可能会有所帮助,我正在为你的问题考虑新的设计
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
class用户
角色表有字段user\u id、party\u id和title作为字符串,title内容可以作为来宾或共同主持人,如果角色作为共同主持人,则用户可以编辑派对模型,如果作为来宾,他们只能阅读等等
然后,为了检查授权,您可以使用