devise and multiple users

609 views Asked by At

I am trying to have Devise create a single User model and have different roles be a separate model. My User model (from rails g devise User) has a email, first name, last name, and role field.

The roles are admin, spectator, competitor. So, I created admin, spectator, and competitor models who all inherit from the User model.

I followed the top answer from devise and multiple "user" models and I can create a user. However, my competitor model migration also has other information such as contest name and location that are not required for the other models. When I do Competitor.create() and put in the necessary information for creating a devise User, the User gets stored in the database even though I have null constraints on the competitor model for contest name and location.

When I do Spectator.all, the recently created competitor data shows up which I thought it shouldn't....

My question is how should I be setting this up so that a competitor user doesn't get created unless his contest name and location is provided.

Another question is why when I do Spectator.all is the competitor's information displaying?

2

There are 2 answers

0
Mostafa Hussein On

If you want an authorization system, so go for CanCan created by Ryan Bates. With CanCan you can have many Roles. I am using it with devise with no problems. See Role Based Authorization and Separate Role Model. And check this ScreenCast about CanCan

0
Aditya Kamatar On

There is a much better way to use devise for multiple users.

Use Rolify Gem

It makes development much easier faster and more secure. You can have the configuration as per your requirement in the question "Single User model and each roles have a separate model"

Tutorial for using Rolify gem + Devise by Rolify Gem developers