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!
精彩评论