Save has_and_belongs_to_many link in basic RoR app
Posted
by
Stéphane V
on Stack Overflow
See other posts from Stack Overflow
or by Stéphane V
Published on 2011-06-21T08:19:13Z
Indexed on
2011/06/21
8:22 UTC
Read the original article
Hit count: 367
I try to learn the has_and_belongs_to_many relationship between my 2 fresh new and simple models Product and Author, where a Product can have many authors and where author can have a lots of products.
I wrote this :
class Author < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :authors
end
In the partial form of view for the products, I have :
<p>Products</p>
<%= collection_select(:product, :author_ids, @authors, :id, :name, :prompt => " ", :multiple => true) %>
but when I hit the update button, I get this strange message I can't resolve myself :
NoMethodError in ProductsController#update undefined method `reject' for "1":String
Rails.root: /home/stephane/www/HABTM
Application Trace | Framework Trace | Full Trace
app/controllers/products_controller.rb:63:in block in update'
app/controllers/products_controller.rb:62:in
update'
Request
Parameters: {"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"2GlTssOFjTVZ9BikrIFgx22cdTOIJuAB70liYhhLf+4=", "product"=>{"title"=>"Le trésor des Templiers", "original_title"=>"", "number"=>"1", "added_by"=>"", "author_ids"=>"1"}, "commit"=>"Update Product", "id"=>"1"}
What's wrong ? Is there a problem with :product_ids... I saw on internet I had to pu a "s" but I'm not sure of what it represents....
How can I link the table authors_products to the key which is given back by the drop-down menu ? (here "author_ids"=>"1") Thx !
© Stack Overflow or respective owner