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
Related
In rails_admin wiki with Devise it has example with only one model. I have two models Admin and Owner. I will define their roles with cancancan.
Is it possible to make more than one model, which uses Devise, able to login to rails_admin dashboard? I just can't find any examples.
Using only one model with diffrent roles may be bad solution, because there each of them will have very different attributes.
The only method I have found is to use inheritance.
I have Admin and User models in which I want to use my rails_admin. I have created Person model which will be parent class for both of them.
Then I added devise for Person with CanCan authorization.
I'm using devise and as per the documentations recommendations I have a User model (for devise) and a Profile model for user data such as names, job etc. Why is it considered "not so good" to just store all these attributes on the devise User model table. Just curious.
Adding fields to tables specified by an external source could result in migration errors in future versions of the gem if the gem changes the table definition.
Think of the users table as an implementation detail of the devise gem. It's generally a bad idea to modify the source of an external library because it makes upgrades difficult or impossible.
Separation of concern! The User model is used by devise (which handles
app auth and access) while the Profile model handles all the business logic.
I've discovered yesterday a tutorial that explains how to implement a role based single user model with Devise, ActiveAdmin and CanCan : http://renisoft.com/devise-activeadmin-cancan-single-user-model/. Yet, I'm new to rails, and I was wondering if it is possible to implement such a solution with roles that have different attributes. For instance, my users will have email, forename, surname, password and will share it with the other roles. But my seller role will have many attributes, my admin and my buyers will have others. Is it something possible to implement with those ruby gems ?
Thanks in advance for your answer.
Users and roles are two different things. Roles haven't got any attributes, but Users have.
However check this:
Rolify is a great Role management library for rails: https://github.com/EppO/rolify
I am developing an app in Rails 3, currently I use Devise as the login and Rails_admin as the admin panel with Paper_trail tracking all changes made by 'user' model... Problem is I have two user models, User and Admin. So a line of code in rails_admin.rb (initiliazer) to setup Paper_trail to track history:
config.audit_with :paper_trail, User
Is there any way to have paper_trail monitor changes made by both User and Admin, or can it only follow one model? I notice that even when it is set like this, and I make a change from within Rails_admin as an admin, the change says it was made by the User with the same ID as the admin that made the change.
The best way to handle this is to install CanCan, remove my Admin Model, and take advantage of using Multiple Roles within a single model for the current situation.
I'm writing a trading system and I have 3 models/roles: Buyers, Sellers and Administrators. I have generated the devise views for each of the models but I would like to use the same sign in, forgotten password pages etc. for them, rather than maintaining 3 sets of views, is there a way of doing this?
The buyer and seller have similar fields (forename, surname, email address, telephone etc.), is it possible to use STI with devise and is it fairly straightforward? At the moment I have 3 separate models with no inheritance.
You can simply have a single User model with a :role attribute and also implement a simple ACL via CanCan or decl_auth (gems). This way they will all sign in etc. via the same session. Devise and CanCan is quite a popular approach and well documented online and in their respective Github wiki's.
For administrators, you can modify your validations to skip on the extra attributes and leave them as blank in the DB.