I'm newbie in ROR.
I need to create many-to-many. There are 2 models: -product (product) -category (categor)
To connect them i make the third model - prodcat (prod_id,cat_id).
In Product controller:
def create @product = Product.new(params[:product]) respond_to do |format| if @product.save params[:prodcat].each do |cat| pms=["prod_id"=>@product.id,"cat_id"=>cat] fl=File.new("/home/bla-bla-bla/out","a") fl.puts pms fl.close @pc=Prodcat.new(pms) @pc.save end format.html { redirect_to(@product, :notice => 'Product was successfully created.') } format.xml { render :xml => @product, :status => :created, :location => @product } else format.html { render :action => "new" } format.xml { render :xml => @product.errors, :status => :unprocessable_entity } end end end
After creation new product there are new records in database, but they are empty:
AREL (1.3ms) INSERT INTO "prodcats" ("prod_id", "cat_id", "created_at","updated_at") VALUES (NULL, NULL, '2010-10-14 15:07:14.203159', 2010-10-14 15:07:14.203159') AREL (1.1ms) INSERT INTO "prodcats" ("prod_id", "cat_id", "created_at", "updated_at") VALUES (NULL, NULL, '2010-10-14 15:07:14.228977', '2010-10-14 15:07:14.228977')
But /home/bla-bla-bla/out isn't empty and has data, that must be in database:
{"prod_id"=>9, "cat_id"=>"1"} {"prod_id"=>9, "cat_id"=>"2"} {"prod_id"=>9, "cat_id"=>"3"} {"prod_id"=>9, "cat_id"=>"4"}
Where is my mistake?