Note that there are some explanatory texts on larger screens.

plurals
  1. PORails forms for has_many through association with additional attributes?
    primarykey
    data
    text
    <p>How can I generate form fields for a <code>has_many :through</code> association that has additional attributes?</p> <p>The <code>has_many :through</code> relationship has an additional column called <code>weight</code>.</p> <p>Here's the migration file for the join table:</p> <pre><code>create_table :users_widgets do |t| t.integer :user_id t.integer :widget_id t.integer :weight t.timestamps end </code></pre> <p>The models look like this:</p> <pre><code>User has_many :widgets, :through =&gt; :users_widgets, :class_name =&gt; 'Widget', :source =&gt; :widget has_many :users_widgets accepts_nested_attributes_for :widgets # not sure if this is necessary Widget has_many :users, :through =&gt; :users_widgets, :class_name =&gt; 'User', :source =&gt; :user has_many :users_widgets accepts_nested_attributes_for :users # not sure if this is necessary UsersWidget belongs_to :user belongs_to :widget </code></pre> <p>For the sake of simplicity, Widget and User only have one field of their own called <code>name</code>, ergo <code>User.first.name</code> and <code>Widget.first.name</code></p> <p><strong>Questions:</strong></p> <ul> <li><p>How would I append a dropdown selection for Widgets with the corresponding weight to the User create/edit form?</p></li> <li><p>How can I dynamically add additional Widget forms to Users or User forms to Widgets to easily add or remove these relationships? The <code>nested_form_for</code> gem seems to be the perfect solution but I haven't been able to get it working.</p></li> <li><p>Apart from the models and the form partials, are there any changes that need to be made to my controller?</p></li> </ul> <hr> <p>Quick note.. I'm not interested in creating new Widgets in the User form or new Users in the Widget form, I only want the ability to select from existing objects. </p> <p>I'm running Rails 3.1 and simple_form 2.0.0dev for generating my forms.</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