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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:46:57+00:00 2026-05-13T12:46:57+00:00

So I have two rails models – user and role – each specified with

  • 0

So I have two rails models – user and role – each specified with a habtm and a join table of users_roles.

When I save from my user form, which has checkboxes for which roles to choose, rails is trying to manually insert a value into the id column of the join table. This doesn’t make sense to me, as the id column should be set to auto_increment and so wouldn’t need a value passed to it. Any time I try to save, I get a MySQL error.

Am I missing something?

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-05-13T12:46:58+00:00Added an answer on May 13, 2026 at 12:46 pm

    if you are using the has_and_belongs_to_many association then you don’t need any model for the join table and the join table should not be created with an id column, it has to contain only the user_id and role_id columns:

       class CreateUsersRolesJoinTable < ActiveRecord::Migration
        def self.up
          create_table :users_roles, :id => false do |t|
            t.integer :user_id
            t.integer :role_id
          end
        end
    
        def self.down
          drop_table :users_roles
        end
      end
    

    See also:

    • http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M001836
    • http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models/tables in my Rails application: discussions and comments. Each discussion has_many
I have two models, TreeNode and User. Each user has_one TreeNode, which is the
In a Rails 3.2 app I have two user models set up using Devise:
I'm using rails 3 and I have two models, venues and areas where each
I am developing a rails application in which I have two models User and
I have two models in my Ruby on Rails app: User and Bookcase. I've
We have two Rails models: Person and Administrator . We're disallowing removal of Administrator
In my Rails 3.2 models directory, I have a folder foo containing two classes:
Lets say I have two Rails models, Users and Lists. Users have_many lists and
Two models (Rails 2.3.8): User; username & disabled properties; User has_one :profile Profile; full_name

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.