可以在 CanCanCan has_many 关系中使用散列功能吗?

Possible to use hash abilities in CanCanCan has_many relationships?

CanCanCan allows us to quickly setup authorization in Rails via abilities. The best practice就是使用Hash条件。然而, scopes/blocks允许更复杂的技能创建。

我想知道是否有办法为 has_many 关系使用散列功能。例如,给定 has_one 创建者和 has_many 管理员(并且都可以更新事件)的事件,我相信这个范围会起作用。

can [:update], Event do |event| 
  event.creator.id == user.id or event.admins.where(user_id: user.id).first.id == user.id 
end

我们可以改为使用哈希吗?

can [:update], Event, creator: { user_id: user.id }
can [:update], Event, admins: { user_id: user.id } #<<< Will this work? Any gotchas?

此外,如果在一行代码中包含散列 OR 会很好,但我找不到一种方法来使它在语法上起作用。谢谢!

使用散列效果很好,虽然看起来你不能设计成一行 ​​- https://github.com/CanCanCommunity/cancancan/wiki/defining-abilities#combining-abilities