0
votes

I am using devise as authorization. And are wondering how the model and database should be. In this ailscasts episode http://railscasts.com/episodes/192-authorization-with-cancan says Ryan that he is using a many to many between roles and users.

My User model:'

class User < ActiveRecord::Base
has_and_belongs_to :roles
has_many :posts
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

My Role model:

class Role < ActiveRecord::Base
has_and_belongs_to :users
end

But how should the roles table be like?

I was thinking creating the role table like:

id
admin - Boolean
moderator - Boolean
author - Boolean 

And then the table roles_users:

id 
role_id
user_id

But why should a user have many roles? When having booleans?

1

1 Answers

0
votes

The roles table would just be:

id
role_name

That way you can add roles without changing schema.