Multiple Devise users or one user and permissions with CanCanCan?

807 Views Asked by At

What is the preferred way to achieve two types of users (Client, Admin) with different attributes and behaviors?

Having two types of users created by Devise or having just one User model and setting permissions via CanCanCan?

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

I would say single model with permissions. I made a detailed response on how to approach this here:

Setting up different User models and registration paths for Devise on Ruby on Rails

0
On

Use only one user and assign a role to the user with a specific column like role_id. Then you can have different roles, each one with a different id.

admin = 1

standard = 2

You can then define a method in the user like:

def admin?
  role_id == 1
end

and in cancan you can use it like that:

def initialize(user) HERE PERMISSIONS FOR NON LOGGED USERS if user HERE PERMISSIONS FOR LOGGED USERS if user.admin? HERE PERMISSIONS ONLY FOR ADMIN end end end

If you are using Rails >= 4.1 you can use an enum for that column.