# Help with join tables and has\_and\_belongs\_to\_many

**URL:** <https://www.ruby-forum.com/t/help-with-join-tables-and-has-and-belongs-to-many/75327>\
**Category:** Rails\
**Created:** [November 9, 2006, 5:45am UTC](https://www.ruby-forum.com/t/help-with-join-tables-and-has-and-belongs-to-many/75327 "2006-11-09T05:45:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [November 9, 2006, 5:45am UTC](https://www.ruby-forum.com/t/help-with-join-tables-and-has-and-belongs-to-many/75327/1 "2006-11-09T05:45:33Z")

</div>

Hi,

I have a user model and a privilege model. They have the  
has\_and\_belongs\_to\_many relationship. Here are the model defs:

class User \< ActiveRecord::Base  
belongs\_to :status  
has\_and\_belongs\_to\_many :privileges  
end

class Privilege \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :users  
end

Now I have a migration script to create the join table and populate a  
few users with some privileges…

class CreateAndInitPrivileges \< ActiveRecord::Migration  
def self.up  
create\_table :privileges\_users do |t|  
t.column :user\_id, :integer, :null =\> false  
t.column :privilege\_id, :integer, :null =\>false  
end

```
admin_priv = Privilege.find_by_name('admin')
user_priv = Privilege.find_by_name('user')

admin_user = User.find_by_email('removed_email_address@domain.invalid')
admin_user.privileges << admin_priv
admin_user.privileges << user_priv

anon_user = User.find_by_email('removed_email_address@domain.invalid')
anon_user.privileges << user_priv # BOMB HERE

```

end

def self.down  
drop\_table :privileges\_users  
end  
end

The line  
anon\_user.privileges \<\< user\_priv  
bombs because Rails tries to insert a row into privileges\_users table  
with id of 1, but that id is already taken because of the previous  
admin\_user.privileges \<\< admin\_priv  
admin\_user.privileges \<\< user\_priv  
calls.

What’s going on? Why is Rails not using the next highest id?

Thanks for the help,  
– Christopher

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [November 9, 2006, 5:51am UTC](https://www.ruby-forum.com/t/help-with-join-tables-and-has-and-belongs-to-many/75327/2 "2006-11-09T05:51:33Z")

</div>

On 11/9/06, Christopher J. Bottaro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What’s going on? Why is Rails not using the next highest id?

I do not know the why of it, but I hit it myself a few days ago.  
Unless you really need that id column in the privileges table, use

create\_table(:privileges\_users, :id=\>false) do |t|  
t.column :user\_id, :integer, :null =\> false  
t.column :privilege\_id, :integer, :null =\>false  
end

The table will be created without the id column and your inserts will  
work.

---

<div class="post-metadata">

**Author:** ![Christopher\_JSBott](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/ee7513/32.png) [@Christopher\_JSBott](https://www.ruby-forum.com/u/Christopher_JSBott)\
**Post date:** [November 9, 2006, 5:59am UTC](https://www.ruby-forum.com/t/help-with-join-tables-and-has-and-belongs-to-many/75327/3 "2006-11-09T05:59:34Z")

</div>

Awesome, that worked perfectly! And to think I was about to turn in  
for the night… 😉

Thank you very much.
