Question

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

  • All 3 user types can have access to different functionality of the application
  • A Staff can be also be a Client.
  • An Admin can also be a Staff or can be a Client or both. An Admin can be a Client without being a Staff.
  • Staff, Admin will have access to the application (username/password). Client may or may not have access to the application.

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

  • The ServiceInstance is "provided" to a user who is a Client (Staff and Admin can be Clients also)
  • The ServiceInstance is "performed" by a "Staff" (Admin can be a Staff also).

Any suggestion/feedback is highly appreciated.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top