Note that there are some explanatory texts on larger screens.

plurals
  1. POSave has_and_belongs_to_many link in basic RoR app
    primarykey
    data
    text
    <p>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.</p> <p>I wrote this :</p> <pre><code>class Author &lt; ActiveRecord::Base has_and_belongs_to_many :products end class Product &lt; ActiveRecord::Base has_and_belongs_to_many :authors end </code></pre> <p>In the partial form of view for the products, I have :</p> <pre><code>&lt;p&gt;Products&lt;/p&gt; &lt;%= collection_select(:product, :author_ids, @authors, :id, :name, :prompt =&gt; " ", :multiple =&gt; true) %&gt; </code></pre> <p>but when I hit the update button, I get this strange message I can't resolve myself :</p> <p>NoMethodError in ProductsController#update undefined method `reject' for "1":String</p> <p>Rails.root: /home/stephane/www/HABTM Application Trace | Framework Trace | Full Trace app/controllers/products_controller.rb:63:in <code>block in update' app/controllers/products_controller.rb:62:in</code>update' Request</p> <p>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"}</p> <p>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....</p> <p>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 !</p> <p>More info : May be solved with this info, but still no saving of the relationship :</p> <pre><code>collection_select("sales_agent", "customer_id", @customers, "id", "name") </code></pre> <p>Assuming you had a customer model with an ID attribute and a name attribute, this would produce exactly the above code. So looking at the values we pass into the collection_select call:</p> <ul> <li>The first parameter is the model that contains one element from the collection (eg. sales_agent)</li> <li>Next is the field name in the model that refers to the collection element it contains (eg. customer_id)</li> <li>Next is the variable containing the collection of items that we want to list (eg. @customers)</li> <li>Next is the value attribute of the option tag (eg. the customer id)</li> <li>Next is the display attribute of the option tag (eg. the customer name)</li> </ul> <p>So I now wrote </p> <pre><code>&lt;p&gt;Products&lt;/p&gt; &lt;%= collection_select(:author, :author_id, @authors, :id, :name, :prompt =&gt; " ", :multiple =&gt; true) %&gt; </code></pre> <p>and it worked, but without saving the link, for the moment... (only the update of the normal fields are saved, not the relationship :-( </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload