Search code examples
ruby-on-rails-3.2rolify

How to return string of role names with rolify


I'm trying to return the name of all the roles a user has in the following format:

'rolename1','rolename2','rolename3'

So that I can user the has_all_roles? method from rolify.

I've tried collecting the role names like this:

user.roles(:select => :name).collect(&:name)

But that returns an array:

["rolename1","rolename2","rolename3"]

the has_all_roles? method wants them like this:

user.has_all_roles?('rolename1','rolename2','rolename3')

I'm sure I'm missing something very simple.


Solution

  • Capture the result of the collect to a variable and turn the captured array into a list of arguments.

    roles = user.roles(:select => :name).collect(&:name)
    user.has_all_roles?(*roles)