1
votes

In my rails application, my Users have many Projects through Participants.

Currently I am using CanCan for authorizations. I have done some research and can't quite figure out how to do the following.

In my ability.rb file I want to provide :manage abilities to to Users on Projects. Currently that looks like:

can :manage, Project if user.has_role? :silver

My Participants table has user_id and project_id. How do I only give those manage capabilities to users that are associated with a project via the participants table?

1

1 Answers

0
votes

I am currently using the following as a solution. I would prefer using a solution native to devise or cancan.

In the model.

def is_participant?(participant)
  self.participants.exists?(:project_id => participant)
end

And in my view.

<% if current_user.is_participant?(@project.id) %>