views:

48

answers:

1

Hi,

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

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:

LucaM