Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique IDs between Users and Admins with Devise Rails

I have installed Devise and created Users and also 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 the Admins it creates a different table, and the potential arises that the admins and regular users could have the same ID's.

So how exactly do I got about grabbing information from users and admins? Say for example I want to display all users? Do I have to traverse the users table and then the admins table?

Or if I am displaying a post. How will I know which table to look for to get the user or admin info?

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

I hope this makes sense haha

like image 545
Dan Avatar asked Feb 26 '12 04:02

Dan


1 Answers

I highly recommend using the Single Table Inheritance (STI). You could do the STI by adding a column named type with a string datatype in the users table and create an admin model as well as a normal_user model both models will inherit from the user model of the devise gem.

class NormalUser < User
end

class Admin < User
end

The type column is a reserved word which will hold a value either NormalUser or Admin according to the user type you created.

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

like image 167
mohamagdy Avatar answered Nov 07 '22 06:11

mohamagdy