Unique identifiers between users and admins using Rails

I installed Devise and created Users as well as Admins using Option 1 from this tutorial https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-role

Now I need a little help. When adding admins, another table is created, and it is likely that administrators and ordinary users may have the same identifiers.

So, how can I get information from users and administrators? Say, for example, I want to display all users? Do I need to move the user table and then the admin table?

Or if I show a message. How do I know which table to look for to get user or admin information?

I know that I can just add a role column to the user table, but I wanted to avoid this.

I hope that makes sense haha

+5
ruby-on-rails devise
Feb 26 '12 at 4:11
source share
1 answer

I highly recommend using single page inheritance (STI). You can do STI by adding a column named type with type string in the users table and create the admin model as well as the normal_user model, both models inherit the devise gem user model.

 class NormalUser < User end class Admin < User end 

The type column is a reserved word that will contain the NormalUser or admin value in accordance with the user type you created.

To create an administrator, use Admin.create(...) and to create a normal user, NormalUser.create(...) , where the dots are the admin and NormalUser

+13
Feb 26 2018-12-12T00:
source share



All Articles