Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:32:00+00:00 2026-06-15T16:32:00+00:00

I am working with three tables; users, roles, and roles_users in a HABTM setup.

  • 0

I am working with three tables; users, roles, and roles_users in a HABTM setup.

I am trying to display all users with their roles.

user.rb Model

class User < ActiveRecord::Base

  has_and_belongs_to_many :roles
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, # :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email,:username, :password, :password_confirmation, :remember_me,     :role_ids
  # attr_accessible :title, :body

  def role?(role)
    return !!self.roles.find_by_name(role.to_s.camelize)
  end

end

role.rb Model

 class Role < ActiveRecord::Base
  attr_accessible :role_name
   has_and_belongs_to_many :users

end

users_controller.rb Controller

class UsersController < ApplicationController
 # GET /users
  # GET /users.xml

#load_and_authorize_resource

  def index
   #  @user = User.find(params[:id])
    #@user.roles
    @users = User.all
    @roles = Role.all
    respond_to do |format|
      format.html # index.html.erb
  format.xml  { render :xml => @users }
end
  end

  # GET /users/1
  # GET /users/1.xml

 def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @user }
    end
  end

I can create a new user and assign a role to him. I can edit the user and change his role. and I can use show to see the individual users role.

The problem is with the index.html.erb file where I am trying to show All users and their roles.

Here is the code.

../views/users/index.html.erb

Listing users

<table>
  <tr>
     <th>ID</th>
    <th>Email</th>
    <th>Username</th>
    <th>Role    </th>
     <th>Role ID</th>>
  </tr>

  <% @users.each do |user| %>
    <tr>
     <td><%= user.id %></td>
         <%= current_user = user.id %>
      <td><%= user.email %></td>
      <td><%= user.username %></td>
     <td> <%= User.find(user.id).roles %> </td>
      <td><%= link_to 'Show', user %></td>
      <td><%= link_to 'Edit', edit_user_path(user) %></td>
      <td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
    </tr>
  <% end %>
</table>

<br />

This produces this output:

john_user@nomail.com    john     [#<Role id: 1, role_name: "basic_user", created_at: "2012-12-06 18:03:43", updated_at: "2012-12-06 18:03:43">] Show    Edit    Destroy

So it is giving me the entire Roles row instead of just the role_name.

I have tried User.find(user.id).roles.role_name, User.find(user.id).roles[1], and several other possibilities and cannot figure out how to just get the role name only to appear.

What is the proper syntax to get just the ‘role_name’ to display like:

john_user@nomail.com    john     basic_user        Show   Edit  Destroy

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:32:01+00:00Added an answer on June 15, 2026 at 4:32 pm

    you can just access it directly:

    <td> <%= user.roles.collect(&:name).join(",") %> </td>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working in Rails 2. I have three tables: users , lms_users and
I am trying to create three tables: Users username userID Categories category categoryID userID
I have three tables that I am working with User, Application, ApplicationAdministrator. ApplicationAdministrator is
I am working with three tables in a db. The first table is labeled
I'm working with three tables, and for simplicity's sake let's call them table A,
We have three developers and one tester all working against the same database. We
I'm working with secure application roles to grant access to users. In its dumbest
I'm having problems making @Column(name=example) working. I've got a User class: @Entity public class
I'm been working on a GetRuleViolations() method for my User class, but I'm getting
I have three tables setup: POSTS, DISCUSSIONS, and COMMENTS. The layout is as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.