我正在使用CanCan 1.6.10和Rails 3.2.13
我正在为我的员工(而不是用户)模型设置我的能力。我有:
class Ability
include CanCan::Ability
def initialize(staff)
if staff.role? :driver
can :read, User.joins(:orders => {:delivery_slot => :driver}).where("driver_id = ?", staff.id)
我已经在控制台中测试了连接,它返回了7条记录。当我测试它的能力时,它无法返回任何:
User.accessible_by(Ability.new(Staff.find(7)))
上一个查询.to_sql中的SQL显示为:
SELECT `users`.* FROM `users` WHERE (1=0)
而User.joins(:orders => {:delivery_slot =>:driver})。其中(“driver_id =?”,7)给出:
SELECT `users`.* FROM `users` INNER JOIN `orders` ON `orders`.`user_id` = `users`.`id` INNER JOIN `delivery_slots` ON `delivery_slots`.`id` = `orders`.`delivery_slot_id` INNER JOIN `staff` ON `staff`.`id` = `delivery_slots`.`driver_id` WHERE (driver_id = 7)
答案 0 :(得分:7)
您是否尝试过hash of conditions而不是明确加入?
can :read, User, :orders=>{:delivery_slot=>{:driver=>{:id => staff.id}}}