I\'m new to rails, and I\'m working on my second rails app.
The app will have different roles for users, but some users will have multiple roles.
Every user of t
This is the basic setup, for the declarative authorization gem, I use. But you could just use this as is without the gem, if your authorization requirements aren't more than asking the kind of Roles the User has.
It does require a roles
table, and such, so that might not really be your fancy.
class Role < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :roles
def role_symbols
roles.map { |r| r.title.to_sym }
end
def admin?
has_role?(:admin)
end
# include more role booleans or write some ruby magic to be DRY
# ...
def has_role?(r)
role_symbols.include?(r.to_sym)
end
end
# give or take
user = User.new
user.roles << Role.new :title => "admin"
user.roles << Role.new :title => "artist"
user.role_symbols # => [:admin, :artist]
user.admin? # => true
user.has_role?(:artist) # => true