Search code examples
ruby-on-railsruby-on-rails-5rolify

Rolify not have a particular role check condition in rails


I am using rolify for assign various roles to a user. Like admin, professor, student, staff.

I am to define a condition where I have to specify that this particular condition is true for only those users who are not admin.

I know how to check if a user is admin

u=User.first
u.has_role?(:admin)

But I not able to figure out how to check the user doesn't have the role of admin?

Please help me get a solution to this.


Solution

  • you can implement this method in user model, this means that the user has other roles but not admin

    def has_not_role?(role)
      roles.where.not(name: role).exists?
    end