简体   繁体   English

Rails,cancancan的能力

[英]Rails, abilities with cancancan

I am using cancancan to define abilities for my demo restaurant app. 我正在使用cancancan定义演示餐厅应用程序的功能。 Each restaurant has_many employees and each employee has user_id and role attributes. 每个餐厅都有很多员工,每个员工都有user_id角色属性。 I would like to allow a user to edit a restaurant only if this restaurant has an employee with user_id as the current_user.id and his role is 'manager'. 我想允许用户编辑餐厅只有这家餐厅有USER_ID作为current_user.id雇员和他的角色是“经理”。 My problem is that this role could be given to a lot of employees and when I find them by 我的问题是,这个角色可以赋予很多员工,当我找到他们时,

can :edit, Restaurant do |restaurant|
    restaurant.employees.where(role: 'Manager').id=user.id 
end

I would get an array of all managers and this code would not return true. 我将得到所有管理器的数组,并且此代码不会返回true。 Any ideas of how to implement this :? 关于如何实现这一点的任何想法:? Thanks! 谢谢!

You're probably looking for something more like this: 您可能正在寻找更像这样的东西:

can :edit, Restaurant do |restaurant|
  restaurant.employees.where(role: 'Manager', user_id: user.id).exists?
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM