hmt problem

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-----------------------------------------

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: <ul>   <% @user.roles.each do |role| %>     <li><%= role.name %></li>   <% end %> </ul>

A partial would work well for this list.

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