User.roles -- How do you list the role names of the user?

Hello,

I want to list out the role names that belong to a user. I have
user.roles as the table, so I’m interested in the “name” column on that
roles table. If I want to separate the names using a comma [ e.g. using
.join(", ") ], how would I do it?

For instance:

<% for role in user.roles %><% role.name %><% end %>

displays all of the roles of the user, but without the comma. There must
be a better way to do it (a less tedious way), and also separate them
with commas?

Any idea?

Bob S. wrote:

displays all of the roles of the user, but without the comma. There must
be a better way to do it (a less tedious way), and also separate them
with commas?

Any idea?

What happens when you do user.roles.join( ‘,’ ) ?

You may need to explain more about the object model. Is this part of an
ORM library? Og? Sequel? ActiveRecord?

–
James B.

“Tear it up and start again.”

  • Anonymous

Voila

<%= user.roles.join(", ") %>

http://www.ruby-doc.org/core/classes/Array.html#M002205

Jake

James B. wrote:

Bob S. wrote:

I want to list out the role names that belong to a user.
<% for role in user.roles %><% role.name %><% end %>
What happens when you do user.roles.join( ‘,’ ) ?
You may need to explain more about the object model. Is this part of an
ORM library? Og? Sequel? ActiveRecord?

It’ll be a Rails question. Please take Rails questions to the Rails
list.

The OP wants <%= user.roles.map(&:name)*’,’ %>

Clifford H…

Jacob Basham wrote:

Voila

<%= user.roles.join(", ") %>

class Array - RDoc Documentation

Jake

Hi Jake, I tried that – and ended up with a pound sign (#)

I’m guessing that brings up an array, and I’ll have to incorporate the
“name” in there somehow

e.g. user.roles[name].join(", ")

(which is probably totally wrong)

any ideas guys?

Ahh yes, I missed that part!

Clifford H. wrote:

James B. wrote:

Bob S. wrote:

I want to list out the role names that belong to a user.
<% for role in user.roles %><% role.name %><% end %>
What happens when you do user.roles.join( ‘,’ ) ?
You may need to explain more about the object model. Is this part of an
ORM library? Og? Sequel? ActiveRecord?

It’ll be a Rails question. Please take Rails questions to the Rails
list.

The OP wants <%= user.roles.map(&:name)*’,’ %>

Clifford H…

Clifford! Thank you, man!

I didn’t see your solution before. You got it. Thank you, thank you,
Clifford :slight_smile: