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

**URL:** <https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686>\
**Category:** Ruby\
**Created:** [November 9, 2007, 6:58am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686 "2007-11-09T06:58:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 6:58am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/1 "2007-11-09T06:58:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 7:11am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/2 "2007-11-09T07:11:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 7:16am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/3 "2007-11-09T07:16:55Z")

</div>

Voila

\<%= user.roles.join(",&nbsp;") %\>

[http://www.ruby-doc.org/core/classes/Array.html#M002205](http://www.ruby-doc.org/core/classes/Array.html#M002205)

Jake

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 7:21am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/4 "2007-11-09T07:21:35Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 7:23am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/5 "2007-11-09T07:23:13Z")

</div>

Jacob Basham wrote:

> Voila
> 
> \<%= user.roles.join(",&nbsp;") %\>
> 
> [class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html#M002205)
> 
> 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(",&nbsp;")

(which is probably totally wrong)

any ideas guys?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 10, 2007, 2:41am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/6 "2007-11-10T02:41:19Z")

</div>

Ahh yes, I missed that part!

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [November 9, 2007, 7:27am UTC](https://www.ruby-forum.com/t/user-roles-how-do-you-list-the-role-names-of-the-user/119686/7 "2007-11-09T07:27:39Z")

</div>

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 🙂
