Many to Many relationship entry insert bug?

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 - This website is for sale! - ruido blanco Resources and Information.
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

    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