Nausik Nausik - 2 months ago 11x
Ruby Question

Adding default role to Devise user model

While trying to add roles-based authentication (using CanCan and Devise) to my project I found that there are two ways to save roles: the first one is "Has and Belongs_to" way and the second one is just saving role in new field in users table.

So, the question is, how can I define deafult user role in the first way and which way should I choose to define deafult role in the second one (setting default role in migration or editing Devise's user controller?)

Also, should I use this method or is it better to use gem instead?

P.S. I've already read Tony Amoyal's tutorial but didn't found an answer there.


This situation described in Rails AntiPatterns book:

Short answer is: use field in users table, set default role using migrations. This way is much simpler. You should not use complex solution just because it can possibly better suit your future needs.