Ask A Question

Notifications

You’re not receiving notifications from this thread.

Single Devise User Model or Multiple Devise Models?

ronald chacon asked in Gems / Libraries

Hi all,

I am planning to create an application that will have several types of users with a couple shared fields and many unrelated fields. What would be my best option?

After doing some research, it seems the most common way is to use Devise + Rolify + CanCanCan/Pundit.

My question is, for each type of user, do I just create different profile models? Or would it better to create a polymorphic association for different profile models?

Reply

The main downside of different models is that you are required to do separate login pages for each (and a lot of confusing links as to who signs in where). Roles take care of this and having just a single User model.

If you've got a bunch of unrelated fields, I would associate the other models with it. So maybe you're building something like Foursquare that has a user, but the user has a personal profile as a reviewer but they also have company profiles for the businesses they own. In that case, you'd just create separate profiles for the companies. Make sense?

Reply

Hi Chris,

So lets say I have a User. They can be a Consumer and/or Seller. In this case I would use roles to define what they can be.

In terms of the profile, would I create 2 different profile models? and associate it with the user? And then use the role attribute to determine the pages they can view?

Reply

You won't necessarily need roles for this because simply the creating of associated records would give you the information if the user is a consumer, a seller, or both.

I would make a User model, a Consumer model, and a Seller model like so:

User
has_one :consumer
has_one :seller

Consumer
belongs_to :user

Seller
belongs_to :user

Then from the User you can check the associations simply enough to determine which type of user they are and give them access to whichever parts of the site you need. You might consider caching a role field on the user for quicker lookups, but that's an optimization you can add later.

Reply

This makes total sense now. I was overthinking this scenario. Thanks Chris.

Reply

Always good to talk those out with someone because if it seems overly complicated, it probably is! :)

Reply

Yep! It made more sense in head after reading your posts. Also in terms roles, when would be a good scenario to use roles?

Reply

An example on the forum here would be "Site Admin" (like me) "Moderator" (someone who can clean up things, delete spam, etc) and "Subscriber" (your normal users). They'd each have a bit more access to do things, but they're all the same users at the end of the day with basically no differences on the database level.

Reply

Quick question Chris what happen if you have different user and different role like this.
This is the users like me the owner of the services.
"App Owner" User Model
Roles:
Super Admin
Moderator
This is the users that are using the service

User Model + Customer User Model
Roles:
Manager
Admin
Vendors
Services
more ...
This is the client of my customer
User Model + Client User Model
Roles:
Manager
General Manager

What techniques will you use for this?

This what I did.

User Model
has_one :client_user
has_one :customer_user

ClientUser
belongs_to :user

CustomerUser
belongs_to :user

And Role with Cancancan for the User

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.