Forum: Ruby on Rails Many to Many relationship entry insert bug?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A878ddd72afd550916074d25f5e94cc2?d=identicon&s=25 wheels (Guest)
on 2007-07-22 03:22
(Received via mailing list)
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(

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:
<li><input id="company_category_ids_24" name="company[category_ids][]"
type="checkbox" value="24" /><label
for="company_category_ids_24">Sports and Recreation</label></li></ul>

and the controller:
    @company.updated_on =
    @company.created_on =

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
  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
  SQL (0.000300)   INSERT INTO categories_companies (`id`,
`category_id`, `company_id`) VALUES (22, 22, 1)
  categories_companies Columns (0.002360)   SHOW FIELDS FROM
  SQL (0.000276)   INSERT INTO categories_companies (`id`,
`category_id`, `company_id`) VALUES (23, 23, 1)

Any ideas how to fix this?

B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (dcmanges)
on 2007-07-22 20:31
(Received via mailing list)
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

Also, you don't need to set created_on and updated_on in your
controller, Rails will do that for you.

Dan Manges
This topic is locked and can not be replied to.