MicFin
MicFin

Reputation: 2501

Rolify has_role? returning true after remove_role

I would like to do the following

User1 = User.find(1)
User2 = User.find(2)

Add a role to users

User1.add_role :teacher
User2.add_role :teacher

User1.has_role? :teacher
>> true

User2.has_role? :teacher
>> true

Delete a role from a specific user

User1.remove_role :teacher

User1.has_role? :teacher
>> false

User2.has_role? :teacher
>> true

I am having a problem because currently

User1.has_role? :teacher 

is returning TRUE even after a remove. However when I do User1.roles it returns an empty array as expected and has_any_roles? returns false as expected. Why is it returning TRUE even after the role is removed?

Upvotes: 0

Views: 399

Answers (1)

Carlos Roque
Carlos Roque

Reputation: 458

you need to reload the user before checking if it has a role again

    User1.remove_role :teacher
    User1.has_role? :teacher
    -> true
    User1.reload

    User1.has_role? :teacher
    -> false

Upvotes: 1

Related Questions