M.S.
M.S.

Reputation: 97

Rails user model with multiple roles

I'm new to rails and I want to understand how to properly define an User model and its roles/types. In the app that I am writing I have 3 types of users: Client, Staff, Admin with the following functionality constraints

The application also has a Service (the definition of the service: name, price, etc) and a ServiceInstance (the sold service) model.

Any suggestion/feedback is highly appreciated.

Upvotes: 0

Views: 880

Answers (1)

Pierre Pretorius
Pierre Pretorius

Reputation: 2909

It sounds like Staff, Admin & Clients will be key concepts for your application. I would create a model for each of these, and have a one to one (has_one or belongs_to) relationship with User. Then you could add validations on User to allow/disallow the combinations of staff, admin & client.

ADDITION: This means that user is still the model that represents someone that can log into your system. If this user is a client and staff then it has these two models associated.

ServiceInstance has an association to Client called 'provided_by'. ServiceInstance has an association to Staff called 'performed_by'. Of course Client and Staff actually indicate a user, but a user in a specific context/role.

If ever you need to show a list of clients, staff or admins, you simply query that table, but print the associated user information. If you need to show a list of all users, query the users table and show an icon or something for whether they are a client, staff, admin or combination of them.

ADDITION: Thus your comment of a user that is staff and a client, will be in the staff and client table since that user has both roles.

Upvotes: 1

Related Questions