# Hmt problem

**URL:** <https://www.ruby-forum.com/t/hmt-problem/133352>\
**Category:** Rails\
**Created:** [March 30, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/hmt-problem/133352 "2008-03-30T20:20:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [March 30, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/hmt-problem/133352/1 "2008-03-30T20:20:58Z")

</div>

Hello…hope someone can help. I have a has\_many through relationship  
set up but am not able to get it to work properly. As you can see I have  
users, roles and a user\_roles table. To verify things are working I  
created an ‘intro’ page with associated action in an ‘admin’ controller.  
The action simply finds the name of the user that’s logged in and should  
display their role name.

@user = User.find(session[:user]).roles.name

The trouble is it just displays the word ‘Role’ on the intro page not  
the users role name. If I change it to

@user = User.find(session[:user]).username  
it will display the username properly, anyone know why this is  
happening?

Thanks…Bill

-----------------------DETAIL-----------------------------------------  
mysql\> desc roles;  
±------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| name | varchar(255) | YES | | NULL | |  
±------±-------------±-----±----±--------±---------------+

mysql\> desc users;  
±--------------±-------------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±--------------±-------------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| username | varchar(255) | YES | | NULL | |  
| password\_salt | varchar(255) | YES | | NULL | |  
| password\_hash | varchar(255) | YES | | NULL | |  
±--------------±-------------±-----±----±--------±---------------+

mysql\> desc role\_users;  
±--------±--------±-----±----±--------±---------------+  
| Field | Type | Null | Key | Default | Extra |  
±--------±--------±-----±----±--------±---------------+  
| id | int(11) | NO | PRI | NULL | auto\_increment |  
| role\_id | int(11) | YES | | NULL | |  
| user\_id | int(11) | NO | | 0 | |  
±--------±--------±-----±----±--------±---------------+

class Role \< ActiveRecord::Base  
has\_many :role\_users  
has\_many :users, :through=\> :role\_users  
end

class User \< ActiveRecord::Base  
has\_many :role\_users  
has\_many :roles, :through=\> :role\_users  
end

class RoleUser \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :role  
end

class AdminController \< ApplicationController  
def intro  
@user = User.find(session[:user]).roles.name  
end  
end

intro.html  
…  
\<%= @user %\>

…

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [March 30, 2008, 9:37pm UTC](https://www.ruby-forum.com/t/hmt-problem/133352/2 "2008-03-30T21:37:04Z")

</div>

Bill McG wrote:

> @user = User.find(session[:user]).roles.name
> 
> The trouble is it just displays the word ‘Role’ on the intro page not  
> the users role name. If I change it to

The #roles method returns a list of roles. Calling #name on this  
returns what type of object is in the list.

If you want the list of role names, then:

@role\_names = User.find(session[:user]).roles.map &:name

(using the Symbol#to\_proc shorthand).

If you want to show the user and their roles in a view, then in the  
controller:

@user = User.find(session[:user], :include =\> :roles)

and in the view:

User: \<%= @user.name %\>  
Roles:

 \<% @user.roles.each do |role| %\> - \<%= role.name %\>
 \<% end %\>

A partial would work well for this list.

---

<div class="post-metadata">

**Author:** ![musty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/77aa72/32.png) [@musty](https://www.ruby-forum.com/u/musty)\
**Post date:** [March 30, 2008, 10:07pm UTC](https://www.ruby-forum.com/t/hmt-problem/133352/3 "2008-03-30T22:07:40Z")

</div>

Mark…fully understand. Thanks for your help, it works great
