Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:19:32+00:00 2026-05-27T14:19:32+00:00

How can I generate form fields for a has_many :through association that has additional

  • 0

How can I generate form fields for a has_many :through association that has additional attributes?

The has_many :through relationship has an additional column called weight.

Here’s the migration file for the join table:

create_table :users_widgets do |t|
  t.integer :user_id
  t.integer :widget_id
  t.integer :weight

  t.timestamps
end

The models look like this:

User
  has_many :widgets, :through => :users_widgets,
           :class_name => 'Widget',
           :source => :widget
  has_many :users_widgets
  accepts_nested_attributes_for :widgets # not sure if this is necessary


Widget
  has_many :users, :through => :users_widgets,
           :class_name => 'User',
           :source => :user
  has_many :users_widgets
  accepts_nested_attributes_for :users # not sure if this is necessary


UsersWidget
  belongs_to :user
  belongs_to :widget

For the sake of simplicity, Widget and User only have one field of their own called name, ergo User.first.name and Widget.first.name

Questions:

  • How would I append a dropdown selection for Widgets with the corresponding weight to the User create/edit form?

  • How can I dynamically add additional Widget forms to Users or User forms to Widgets to easily add or remove these relationships? The nested_form_for gem seems to be the perfect solution but I haven’t been able to get it working.

  • Apart from the models and the form partials, are there any changes that need to be made to my controller?


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.

I’m running Rails 3.1 and simple_form 2.0.0dev for generating my forms.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:19:32+00:00Added an answer on May 27, 2026 at 2:19 pm

    I will be solving your problem using cocoon, a gem I created to handle dynamically nested forms. I also have an example project to show examples of different types of relationships.

    Yours is not literally included, but is not that hard to derive from it. In your model you should write:

    class User 
      has_many :users_widgets
      has_many :widgets, :through -> :user_widgets
    
      accepts_nested_attributes_for :user_widgets, :reject_if => :all_blank, :allow_destroy => true
    
      #...
    end
    

    Then you need to create a partial view which will list your linked UserWidgets. Place this partial in a file called users/_user_widget_fields.html.haml:

    .nested-fields
      = f.association :widget, :collection => Widget.all, :prompt => 'Choose an existing widget'
      = f.input :weight, :hint => 'The weight will determine the order of the widgets'
      = link_to_remove_association "remove tag", f
    

    In your users/edit.html.haml you can then write:

    = simple_form_for @user do |f|
      = f.input :name
    
      = f.simple_fields_for :user_widgets do |user_widget|
        = render 'user_widget_fields', :f => user_widget
      .links
        = link_to_add_association 'add widget', f, :user_widgets
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate windows form fields based off of a text file that
I have a HQL query that can generate either an IList of results, or
Is there a tool that can generate sequence diagrams from running .NET code?
Is there an IDE out there that can generate XML from XSD? Not Oxygen
How do i repost post data, so that i can generate a print preview
I have a web report that uses a Django form (new forms) for fields
I have this table where I can generate dynamic rows (which has input <type=text
I can add an element to a form like this: $form->addElement($element); However, that will
I can generate my models and schema.yml file based on an existing database. But
In Clearcase I can generate a label for a given set of files and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.