, , , :
EDIT:
. , "" "UserRole".
user.rb
has_many :user_roles, :dependent => :destroy, :uniq => true
has_many :roles, :through => :user_roles, :uniq => true
def has_role?(role_sym)
roles.any? { |role| role.name.underscore.to_sym == role_sym.downcase }
end
role.rb
has_many :user_roles, :dependent => :destroy, :uniq => true
has_many :users, :through => :user_roles, :uniq => true
user_role.rb
# id :integer(4) not null, primary key
# user_id :integer(4)
# role_id :integer(4)
#--
# Relationship
belongs_to :user
belongs_to :role
.rb
def initialize(user)
user ||= User.new
if user.has_role? :Admin
can :manage, :all
else
can :read, :all
end
end
, , -, - :
...
admin = Role.create!(:name => "admin")
standard = Role.create!(:name => "standard")
user1.roles << admin
user2.roles << standard
, user.roles < < [role_name], UserRole, user_id role_id.