dmo*_*oly 4 ruby-on-rails has-and-belongs-to-many cancan
我有以下内容:
has_and_belongs_to_many餐馆的用户模型,反之亦然.that has_and_belongs_to_many用餐,反之亦然.在我的ability.rb文件中,我想指定一个用户只能管理他"has_and_belongs_to"(即餐馆user.restaurants)的餐馆的饭菜.
我现在有这个:
class Ability
include CanCan::Ability
def initialize(user)
# a user can only manage meals of restaurants he has access to
can :manage, Meal do |meal|
meal.restaurant_ids & user.restaurant_ids #...this doesn't work...
end
end
end
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?
我已经咨询了https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks但我仍然不知道.
数组上的"&"运算符将始终返回一个数组.空数组不被视为false,因此它将始终通过.尝试检查它是否存在(非空白).
(meal.restaurant_ids & user.restaurant_ids).present?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
845 次 |
| 最近记录: |