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 7843745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:38:56+00:00 2026-06-02T16:38:56+00:00

im using rails 3.2.3 and am gettign weird values in my app. I have

  • 0

im using rails 3.2.3 and am gettign weird values in my app.

I have 2 models, a user and role, which are a has_and_belongs_to_many relation. The middle table is created with role_id and user_id column and correct name (roles_users).

The thing is, when I assign a user to a role (upon creation through a checkbox) and then view all the users with roles associated to them I get invalid role id(like 75216410 when it sould be just 1 or 2) and the role name is just “Role” instead of admin.

I want to use Devise and CanCan, I’ve set up Devise but this thing keeps me from going.

My roles and users controller has the traditional restfull actions, the exact same ones we get when we scaffold. And since the DB values are correct, I believe this isn’t the issue.

My models are:

user.rb

class User < ActiveRecord::Base
  # Devise modules
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable, :registerable
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable

  #relationships
  has_and_belongs_to_many :roles

  #nested attr
  accepts_nested_attributes_for :roles

  # Accessible (or protected) attr
  attr_accessible :username, :email, :password, :password_confirmation, :remember_me, :role_ids

  # attr_accessible :title, :body


  #validations
  validates :username, :presence => true
  validates :username, :email, :uniqueness => true

end

role.rb

class Role < ActiveRecord::Base
  has_and_belongs_to_many :users
  attr_accessible :name, :user_attributes, :role_id

end

My users and roles views are the same you would get by a scaffolding as well.
My users index:

  <% @users.each do |user| %>
    <tr>
      <td><%= user.username %></td>
      <td><%= user.email %></td>
      <td><%= user.roles.id %></td>    #should display "1"
      <td><%= user.roles.name %></td>  #should display "admin"
      <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 %>

The thing is, this displays, instead of the role id (1), some random number like 69067380. If I refresh, this number changes to another random number. As for the Role name, it always says “Role” instead of “admin”.
This reminds me of a pointer memory address, but i doubt it is the case.

NOTE: When I check the tables with MySQL workbench, everything is fine and all the values in the tables are correct.

Any help is appreciated, thanks in advance

  • 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-02T16:38:57+00:00Added an answer on June 2, 2026 at 4:38 pm

    You user has many roles so when u fires user.roles it will give an array instead of single record try user.roles.first or user.roles.first.try(:name) it display first role.you can iterate role and display them as per your requirement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rails 3.0.3. I have the following route in routes.rb: match user/create_new_password/:reset_password_key =>users#create_new_password, :via=>[:get,
I have inherited an app set up using built using rails using the facebooker
I've employed devise as the user-authentication gem for my rails web-app. Using this pre-generated
Using Rails 3.1.3 and Ruby 1.9.3. I want to give the user a list
I am caching several User objects using Rails.cache.write but when I retrieve it from
This is a very strange problem. I've got a rails app in which I
I am a beginner in using Rails and Git I have InstantRails installed and
I'm using rails 3.1 with Devise for user authentication... I'm having trouble either getting
I am using ruby 1.8.7 and rails 2.3.5 and I am getting this weird
I am using the make_resourceful plugin in my Rails app, and attempting to use

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.