简体   繁体   中英

devise and multiple users

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM