开发者

CanCan complex ability definition

开发者 https://www.devze.com 2023-02-19 21:55 出处:网络
I have a Task object. Tasks belong to a user OR a company. A user can belong to many companies through employmentships (like a regional manager).

I have a Task object.

Tasks belong to a user OR a company.

A user can belong to many companies through employmentships (like a regional manager).

Is there a way that 开发者_如何转开发I can do a check with cancan to see if a task either belongs to the user or one of the companies they belong to?

i.e.

can :manage, Task, do |task|
   task.user_id == user.id || user.companies.each do |company|
     task.company_id == company.id
   end
end


Yes you can, If I understood this well, something like this should work:

can :manage, Task, do |task|
   task.user_id == user.id || user.company_ids.include?(task.company_id)
end

Best!

0

精彩评论

暂无评论...
验证码 换一张
取 消