1

Product controller:

def update
 params[:product][:category_ids] ||= []
 @product = Product.find(params[:id])
 if @product.update_attributes(params[:product])
  redirect_to @product
 else
  render "edit"
 end 

form:

    <% for category in Category.all %>
    <%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %> 
    <%= category.name %>
  <% end %> 

When I look at the log, it looks like it updated the category, but when I go to rails console and did a lookup for the product, it tells me that the category_id is nil so when I do a search for all products in a certain product category, it doesn't return anything because all the category_id is nil.

I have a product and a category model and a simple join table for categories_products. I tried using accepts_nested_attributes_for in the product model and made the respective form changes as well and that didn't work either.

Does anyone know why it doesn't save to the database level? Also in a situation like this, is it better to do it the way I have it here or to do nested attributes? Thanks.

UPDATE

Code from log

Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxx", "product"=>{"remote_image_url"=>"", "name"=>"Test", "manufacturer"=>"Unknown", "category_ids"=>["1"], "price"=>"$25.00", "available"=>"true", "commit"=>"Update", "id"=>"1"}

  Category Load (0.5ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 1 LIMIT 1
  Category Load (0.2ms)  SELECT * FROM `categories` INNER JOIN `categories_products` ON `categories`.id = `categories_products`.category_id WHERE (`categories_products`.product_id = 1 )
  AREL (0.4ms)  DELETE FROM `categories_products` WHERE `categories_products`.`product_id` = 1 AND `categories_products`.`category_id` IN (2)
  AREL (0.4ms)  UPDATE `products` SET `price` = 0.0, `updated_at` = '2011-10-03 16:48:01' WHERE `products`.`id` = 1
  SQL (1.8ms)  COMMIT

Code from product model

attr_accessible :name, :manufacturer, :price, :category_ids, :image, :remote_image_url, :available
has_and_belongs_to_many :categories

Code from category model

has_and_belongs_to_many :products
noob
  • 1,807
  • 2
  • 18
  • 34
  • Could you add the line from your log file where it shows the parameters being posted to the server? – rdvdijk Oct 03 '11 at 17:44
  • Also, could you show the relevant code in your two models? – rdvdijk Oct 03 '11 at 17:46
  • @rdvdijk, please see above update. – noob Oct 03 '11 at 17:55
  • Perhaps you need to make sure the category IDs are integers? They seem to arrive in the params as strings, maybe you need to convert them, but maybe ActiveRecord deals with this for you. – hoff2 Oct 03 '11 at 18:17

1 Answers1

0

I think you can't accomplish what you are trying to do with a "habtm". Take a look at the answer to this question about changing the habtm to a has_many :through association, and using the join table model in your form to update the association.

Community
  • 1
  • 1
rdvdijk
  • 4,400
  • 26
  • 30
  • Since changing it to a has_many :through association, the category ids are being stored in the productcategorization model, but my search functionality is still not looking. I am using meta_search gem and my code looks like this in the product form: <%= collection_select :products_categories_category_id_equals_any, Category.all, :id, :name, :include_blank => true, :prompt => "Select a category" %> – noob Oct 03 '11 at 19:56
  • Your original problem has been fixed, right? Questions on StackOverflow should be reasonably scoped. I advise you to start a new question on this particular subject. (Also [see the FAQ](http://stackoverflow.com/faq#dontask).) Feel free to accept this answer as the solution to your original problem. – rdvdijk Oct 03 '11 at 20:06