I’d like to add several different user types to my rails application. I have 2 different ways I think I could go about this:
I could use the method per the Devise documentation
# Inside the various models
devise :database_authenticatable, :trackable, :timeoutable, :lockable
# Inside your routes
devise_for :model1
devise_for :model2
devise_for :model3
# Inside your protected controller
before_filter :authenticate_model1!
# Inside your controllers and views
model1_signed_in?
current_model1
model1_session
My concern for this method is that I would like to have all types of users log in via the same form and for most cases, all models share most of the same privileges so I don’t want to have to check for each one for model_signed_in? etc. I don’t think I really want to deal with STI either.
Method 2 – Have the one User model and add a “role” column. Then just create the other models and have them belong_to User. This seems like the way to go, but I’m stuck on how to handle the associations in the User model.
Do I just do a has_one for each of the other models? This seems dumb considering it can only have 1 of the others. I feel like I’m missing something simple here…
Whats the way to go?
One thing that you could do is choose your controller based on which user mode you want to login, as devise has separate controllers for each users, e.g member and admin controller.
The best way to go as per your requirement is to add a column role_id to the User model, and create a Role model, which has several fields as per your requirement, that store access information for different things.