pcasa
pcasa

Reputation: 3730

CanCan complex ability definition

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

Upvotes: 1

Views: 855

Answers (1)

jpemberthy
jpemberthy

Reputation: 7533

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!

Upvotes: 4

Related Questions