role model is a gem that lets you add roles to the user as a method and
saves the role into a field on the user table as a bitmask
that is , lets say you have and array like this
ROLES = %w[admin moderator author banned]
this gord in your user model and you have a field in the user table
called roles_mask
as you see you can map the roles in the array as a series of bit
[admin moderator author banned]
0 0 0 0
if a user is admin the in his roles_mask field is this
1000
in binary which translate to 8 in decimal format, if the user is
admin and moderator the value is
1100
and that is a 12
As you can see there is no reason to have a roles table in the proyect
unless you have
like 20 or more roles.
role model saves the roles with a virtual attribute, roles that is in
you user model and is looks like this
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index® }.sum
end
as you can see it maps the array in ROLES to a binary array and saves
it in the roles_mask field of the users table
to read a role it does this
def roles
ROLES.reject do |r|
((roles_mask || 0) & 2**ROLES.index®).zero?
end
end
this returns an array of strings when ever it finds a 1 in the binary
representation of the string array that is saved on the
roles_mask field
On Tue, Sep 14, 2010 at 11:47 AM, Daniel Gaytán <