Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem looks like it's with your first statement in the update method - you're creating a new wine bar record, instead of loading an existing record and updating it. That's why when you examine the record, there's nothing showing of the relationship. Rails <em>is</em> smart enough not to drop/create every record on the list, so don't worry about that.</p> <p>If you're using the standard rails setup for your forms:</p> <pre><code>&lt;% form_for @wine_bar do |f| %&gt; </code></pre> <p>Then you can call your update like this:</p> <pre><code>class WineBarController def update @winebar = WineBar.find(params[:id]) render (@winebar.update_attributes(params[:wine_bar]) ? :update_success : :update_failure) end end </code></pre> <p>You don't need to explicitly update your record with <code>params[:wine_bar][:wine_ids]</code>, because when you updated it with <code>params[:wine_bar]</code>, the wine_ids were included as part of that. I hope this helps!</p> <p><strong>UPDATE:</strong> You mentioned that this doesn't work because of how the forms are setup, but you can fix it easily. In your form, you'll want to rename the input field from <code>wine_bar[wine_ids]</code> to <code>wine_bar[wine_ids_string]</code>. Then you just need to create the accessors in your model, like so:</p> <pre><code>class WineBar &lt; ActiveRecord::Base def wine_ids_string wines.map(&amp;:id).join(',') end def wine_ids_string= id_string self.wine_ids = id_string.split(/,/) end end </code></pre> <p>The first method above is the "getter" - it takes the list of associated wine ids and converts them to a string that the form can use. The next method is the "setter", and it accepts a comma-delimited string of ids, and breaks it up into the array that <code>wine_ids=</code> accepts.</p> <p>You might also be interested in my article <a href="http://kconrails.com/2010/12/07/dynamic-form-elements-in-ruby-on-rails/" rel="nofollow">Dynamic Form Elements in Rails</a>, which outlines how rails form inputs aren't limited to the attributes in the database record. Any pair of accessor methods can be used.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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