Search code examples
ruby-on-railsrolify

rolify multiple role checking should always include 1 more global role?


I check the docs but it only works when include 1 more global role. how can I do multiple role checking without global role?

user.add_role :admin
user.add_role(:user, group)
user.add_role(:mentor, group)
user.has_all_roles? :admin, {:name => :mentor, :resource => group} 
=> true
user.has_all_roles? {:name => :mentor, :resource => group}, {:name => :user, :resource => group}
=> SyntaxError: (irb):27: syntax error, unexpected =>, expecting '}'
user.has_any_role? :admin, {:name => :mentor, :resource => group}, {:name => :user, :resource => group}
=> true
user.has_any_role? {:name => :mentor, :resource => group}, {:name => :user, :resource => group}
=> SyntaxError: (irb):30: syntax error, unexpected =>, expecting '}'

Solution

  • Very annoying, but simply solved! Reason is that by writing

    user.has_all_roles? {...}
    

    you tell ruby: I'm giving a block to this method call... And that's obviously not what you want.

    Simple fix: just add parentheses to your method call:

    user.has_all_roles?( {...} )