我可以轻松地测试用户是否具有某种角色
if user.has_role? :admin
如何获取用户的角色名称? 像
这样的东西users = User.all
user.each{ |user|
puts user.role or users.role_name ?
}
用户模型
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me ,:username,:first_name,:last_name
# attr_accessible :title, :body
end
榜样
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
attr_accessible :name,:id
scopify
end
答案 0 :(得分:7)
你可以使用
user.roles.first.name