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 Manges
http://www.dcmanges.com
On Jul 21, 9:21 pm, wheels
<wheels...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> 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(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:
> <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 =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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---