ruby-on-rails – 使用Rails的CanCan gem来处理has_and_belongs_
发布时间:2020-12-17 02:41:56 所属栏目:百科 来源:网络整理
导读:我有以下内容: has_and_belongs_to_many餐厅的用户模型,反之亦然. 餐馆模型has_and_belongs_to_many用餐,反之亦然. 在我的ability.rb文件中,我想指定用户只能管理他“has_and_belongs_to”(即用户餐馆中的餐馆)的餐馆的饭菜. 我现在有这个: class Ability
我有以下内容:
> has_and_belongs_to_many餐厅的用户模型,反之亦然. 在我的ability.rb文件中,我想指定用户只能管理他“has_and_belongs_to”(即用户餐馆中的餐馆)的餐馆的饭菜. 我现在有这个: 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 最好的方法是什么? 我咨询了https://github.com/ryanb/cancan/wiki/Defining-Abilities-with-Blocks,但我还是不知道. 解决方法
“&”数组上的运算符将始终返回一个数组.空数组不被视为false,因此它将始终通过.尝试检查它是否存在(非空白).
(meal.restaurant_ids & user.restaurant_ids).present? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |