比较好的用户多角色控制,适用于用户角色不多的情况,最好5个以内

 named_scope :email_is, lambda { |email|{  :conditions => (["USERBBSEMAIL = ? ",email] unless email.blank?  )}}

 

好处是不用再添加一个表.


  ROLES = {
      '管理员' => 1,
      '审核员' => 2,
      '评分员' => 4
    }
   
    def reset_role
      self.roles = 0
    end

    def add_role role
      self.roles ^= role
    end
   
    def set_roles roles
      self.roles = 0
      roles && roles.each do |role|
        self.add_role role.to_i
      end
    end

    def has_role? role
      (self.roles & ROLES[role]) == ROLES[role]
    end
   
    def roles_for_show
      roles = []
      ROLES.each do |key,value|
        roles << key if self.has_role? key
      end
      roles.join(" ")
    end

你可能感兴趣的:(用户)