Nausik Nausik - 1 year ago 140
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download