You’re not receiving notifications from this thread.
Devise with multiple User Models with single login form
Hi im trying to have 3 type of devise users but one to have a single login. I need to find a way to make the routes to go to a site/login for all 3 of then.
Im not using the 1 User model with 3 type of User like roles beacuse each of the type of user has he's on role types. example: Admin has 3 rols Admin, Super Admin and Moderator.
Sounds like you'd be better off making one User model and then adding roles to them instead with Rolify and Pundit. I'd definitely recommend doing this.
If you do want to keep the separate models, you have a couple options.
- You can have separate urls and they choose which type of user they are and get directed to the correct devise login URL.
- You could override the controller to search through each model to find the correct user. This solution gets pretty nasty quickly and isn't flexible for future changes. If you still want to do that, you can attempt something like this: http://blog.frankzhao.com.au/2014/12/single-sign-on-for-two-different-user-models-with-devise/
Thank Chris for the quick response. and Yes I saw that one and looks pretty nasty for a solution. On my app im using a User Model and 2 User type ServiceUser and ClientUser that belongs to the User Model. I think this a better solution for getting Devise and Multitenancy working fast and without any hacks.
Chris - just joined gorails. This is awesome! I am new to rails (coding in general) and was watching railscasts, but the videos seem outdated. I just want to make sure I fully understand you solution. I have two types of User Models - Customers and Service Providers. I am trying to figure out how to create two separate models with devise. I tried to do a polymorphic association and was able to get it to work in the rails console, but when it came to manipulating the code on the front end with devise, I could not get it to work. An important feature is for Customers to be able to write reviews for the Service Provider. If I go the route of using Devise and the Pundit gem, will I be able to do utilize that feature? If I did the separate model option and used point #1, how would I do the separate URL? Thank you in advance for your help Chris. Any advice is greatly appreciated!
Hey Benjamin! Welcome to GoRails :)
A couple things here for you:
Polymorphism is talking about relationships and maybe doesn't apply here. An example of polymorphism is having a Review that could apply to a Service Provider OR a Customer. Maybe you want them to be able to rate their customers too (like Uber does internally for example). A polymorphic relationship lets you have the relationship point to various types of models. It sounds like yours is simpler where you will have a customer owns a review and the review points to a service provider.
Creating two separate models with devise is pretty easy:
rails g devise Customer rails g devise ServiceProvider
You'll have different URLs to use in the templates. Like
new_service_provider_registration_path for example. You can run
rake routes to find out what the other urls are after you've generated the models.
Does that make sense?
Chris, thank you for the quick response. It does make sense and I will give your recommendation a try tonight.
Also, you are correct. I am going to set up the relationship in which the customers can only review the service providers.
Chris what do you think of this?
What if a use case the following:
Generate 1 devise Account model
Create 2 Other Models: User and Member
User and Member both inherit from Account
Member has multiple roles(owner, employee, etc.)
Member is representative of a Company, so when a company signs up,
we Create a Member model + accept nested attributes and build a Company model from there
How to have proper devise routes setup for 2 models and tight them to 1 model? I'm trying to do it without any luck so far.
Maybe using this one somehow can help?
Feels I'm getting close
devise_for :agents devise_for :companies
rails g devise User
class Company < User end class Agent < User end
application_controller put this:
devise_group :user, contains: [:agent, :company]
so if using confirmable or something, we still can use
I haven't seen or used
devise_group before! Thanks for sharing this. Looks super useful.
Got here trough searching on google. devise_group does sound super useful!! Thanks a lot for finding this.