# Many to Many relationship entry insert bug?

**URL:** <https://www.ruby-forum.com/t/many-to-many-relationship-entry-insert-bug/110050>\
**Category:** Rails\
**Created:** [July 22, 2007, 3:22am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-entry-insert-bug/110050 "2007-07-22T03:22:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![wheels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wheels](https://www.ruby-forum.com/u/wheels)\
**Post date:** [July 22, 2007, 3:22am UTC](https://www.ruby-forum.com/t/many-to-many-relationship-entry-insert-bug/110050/1 "2007-07-22T03:22:56Z")

</div>

Hi,  
I have a many to many relationship setup for two tables. I’m using  
Rails 1.2.3 with mySQL 5. It is specifying the id column when it  
shouldn’t be.

The details:  
I’m using the Multiple Select Helper([ruido-blanco.net&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;ruido blanco Resources and Information.](http://ruido-blanco.net/blog/)  
rails-multiple-select-helper-plugin).

I have two models, company and categories. They have a many to many  
relationship. Each class has the “has\_and\_belongs\_to\_many” attribute.  
I also created the categories\_companies table. I checked the table and  
it has an id field with the auto-increment attribute. I created the  
tables using migrations.

In the view, I have:  
collection\_multiple\_select(‘company’, ‘category\_ids’,  
Category.find(:all), :id, :name)

and it generates a list of the categories in this format:

- Sports and Recreation

and the controller:  
@company =Company.new(params[‘company’])  
@company.updated\_on = Time.now  
@company.created\_on = Time.now  
@company.save

However, after I save, the categories\_companies table has the id being  
the same number as the category\_id.  
So the id did not start at 1, it starts at whichever category was  
selected. I’m not sure if this bug is because of something I did or  
the plugin code…

This is the server log(NOTE that the id in the categories\_companies  
table is being specified instead of being ignored like the company  
table):  
SQL (0.000318) INSERT INTO companies (`created_on`, `name`,  
`info`, `updated_on`, `url`, `valid`) VALUES(‘2007-07-21 18:00:21’,  
‘honda’, ‘dgf’, ‘2007-07-21 18:00:21’, ‘dfsq’, NULL)  
categories\_companies Columns (0.002207) SHOW FIELDS FROM  
categories\_companies  
SQL (0.000300) INSERT INTO categories\_companies (`id`,  
`category_id`, `company_id`) VALUES (22, 22, 1)  
categories\_companies Columns (0.002360) SHOW FIELDS FROM  
categories\_companies  
SQL (0.000276) INSERT INTO categories\_companies (`id`,  
`category_id`, `company_id`) VALUES (23, 23, 1)

Any ideas how to fix this?

Thanks!  
Tommy

---

<div class="post-metadata">

**Author:** ![wheels](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7bcc69/32.png) [@wheels](https://www.ruby-forum.com/u/wheels)\
**Post date:** [July 22, 2007, 8:31pm UTC](https://www.ruby-forum.com/t/many-to-many-relationship-entry-insert-bug/110050/2 "2007-07-22T20:31:56Z")

</div>

If you’re using has\_and\_belongs\_to\_many, the table for the  
relationship shouldn’t have an id column. In your migration you do:

create\_table :categories\_companies, :id =\> false do  
…  
end

Also, you don’t need to set created\_on and updated\_on in your  
controller, Rails will do that for you.

Dan M.  
[http://www.dcmanges.com](http://www.dcmanges.com)
