views:

43

answers:

1

Hello... Currently I'm using Devise & CanCan which allows me to create Users with Roles using a (Roles_Users) table.

That's nice, but what I want is to Have Projects in my app and for each project for a user to possibly have a role like (Admin, Viewer, etc) IE, roles are not assigned to users but to users based on what projects they are a member of.

Examples:

  • User X belong to Project A with an Admin Role
  • User X belong to Project B with an Guest Role
  • User Y belong to Project B with an Observer Role

What kind of Model would work for this?

Models

Users has_many: projects Projects ? Roles ?

Users_Roles_Projects (user_id, project_id, role_id)

What do you think? I'm a newbie and could use the understanding and thinking from you fine experienced folks. Thanks!

+1  A: 

You should have a look in to has_many :through. This Railscast should get you up and running: http://railscasts.com/episodes/47-two-many-to-many

For example, you could have User has_many Projects through Memberships (I'm sure you can come up with a better name!)

Your Users model would contain the standard user details, the Projects model would contain the project details and presumably you have some Roles model somewhere (I've not used either of the libraries you mentioned so I can't comment in terms of how they work). The key is the Memberships model.

The membership model would contain the userID, projectID and a roleID. In the database there should only be one instance of any given userID and projectID pairing so by storing the roleID along side this pairing you can assign the role to that user on the specified project.

Ash
That's awesome. Are you familiar with CanCan? I'm curious how to use CanCan with the Memberships model above. Ideas?
AnApprentice
I've not used CanCan before, but having a quick look at the Railscast it doesn't look like you can't use it. As far as I can tell, the initialise method only takes a user. Without a reference to a project I can't see how you could apply project specific permissions. Of course I could be wrong - my experience with CanCan is ~15 mins! :-)
Ash