Hi everybody, I'm fairly new to rails and this list. I'm working on a little app and I'm running into a problem with a many-to-many entry insert.
I have two models, Company and Category.
 The company.rb model has:   has_and_belongs_to_many :categories
and the category.rb model has:   has_and_belongs_to_many :companies

When I try to create a new company and associate it with a category, it does not do the insertion into the categories_companies table correctly It tries to specify the id for the categories_companies table when it should not. The id should be auto-incre. instead of being specified. Any ideas what I'm doing wrong?

This is the output from the log:
Parameters: {"company"=>{"name"=>"honda", "info"=>"", "url"=>"robote", "category_ids"=>["5", "6"]}, "commit"=>"Create", "action"=>"create", "controller"=>"company"}
[extra msgs removed]
SQL (0.043682) INSERT INTO companies (`created_on`, `name`, `info`, `updated_on`, `url`, `valid`) VALUES('2007-07-25 22:25:09', 'honda', '', '2007-07-25 22:25:09', 'robote', NULL) categories_companies Columns (0.002555) SHOW FIELDS FROM categories_companies SQL (0.000306) INSERT INTO categories_companies (`id`, `category_id`, `company_id`) VALUES (5, 5, 2) categories_companies Columns (0.002321) SHOW FIELDS FROM categories_companies SQL (0.000283) INSERT INTO categories_companies (`id`, `category_id`, `company_id`) VALUES (6, 6, 2)
  SQL (0.000778)   COMMIT


The tables are:
categories: id, name
companies id,name,info,...
categories_companies: id, company_id, category_id
migrate entry:
    create_table :categories_companies do |t|
      t.column :company_id, :integer
      t.column :category_id, :integer
    end

My view for the create form is:

<form action="/company/create" method="post">
<p><label for="name">Name</label><br/>
<input id="company_name" name="company[name]" size="30" type="text" / ></p>
<p><label for="url">url</label><br/>
<input id="company_url" name="company[url]" size="30" type="text" /></p>
<p><label for"categories">Categories</label></p>
<ul><li><input id="company_category_ids_1" name="company[category_ids] []" type="checkbox" value="1" /><label for="company_category_ids_1">Computer Software</label></li> <li><input id="company_category_ids_2" name="company[category_ids][]" type="checkbox" value="2" /><label for="company_category_ids_2">Computer Hardware</label></li>
.....
<input name="commit" type="submit" value="Create" />
</form>

Controller for company create(i tried both the = and << for the category_ids and without, all add it in with the bad sql command):
    @company =Company.new(params['company'])
    #check which categories were checked
    [EMAIL PROTECTED] = params[:company][:category_ids]
    @company.category_ids << params[:company][:category_ids]
    @company.updated_on = Time.now
    @company.created_on = Time.now

    @company.save


Thanks,
Tommy

_______________________________________________
Sdruby mailing list
[email protected]
http://lists.sdruby.com/mailman/listinfo/sdruby

Reply via email to