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 7183795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:04:07+00:00 2026-05-28T18:04:07+00:00

I’m trying to implement a has_many :through join in Rails 3 (with Formtastic) and

  • 0

I’m trying to implement a has_many :through join in Rails 3 (with Formtastic) and I’m a little stuck. I have the models set up as follows:

Models:

class Project < ActiveRecord::Base
has_many :employees, :through => :teams
has_many :teams

class Employee < ActiveRecord::Base
has_many :projects, :through => :teams
has_many :teams

class Team < ActiveRecord::Base
belongs_to :project
belongs_to :employee

And this line gives me a multi-select box in the projects view that allows employees to be selected:

View:

<%= f.input :employees, :as => :select %>

So far this gets the job done, but what I’d LIKE to have is a separate dropdown box to select each employee’s name, then their role in the project. I can’t figure out the form code to get me there…


EDIT:

As suggested, I’ve gotten the code from Railscast 197: Nested Model Forms working and it’s part-way there. This is what I have in the view:

<%= f.semantic_fields_for :employees do |builder| %>
     <%= render 'employee_fields', :f => builder %>
<% end %>
<%= link_to_add_fields "add employee", f, :employees %>

and the ’employee_fields’ partial:

<p class="fields">
    <%= f.input :name, :as => :select, :collection => Employee.find(:all) %>
    <%= f.hidden_field :_destroy %>
    <%= link_to_remove_fields "remove", f %>
</p>

But right now this creates a new employee rather than a new team (project-employee join record), so I think it’s acting as a has_many rather than a has_many :through. How can I edit this so that the :name input above adds a record to project[employee_ids][]?

  • 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-28T18:04:09+00:00Added an answer on May 28, 2026 at 6:04 pm

    Oh my god, I finally got this thing to work. Here’s the relevant code, minus the bits added to make the form add and remove fields dynamically:

    _form.html.erb

    <%= semantic_form_for @project do |f| %>
        .
        .
        <%= f.semantic_fields_for :teams do |builder| %>
            <input id="project_teams_none" name="team[employee_ids][]" type="hidden" value="" />
            <%= render 'team_fields', :f => builder %>
        <% end %>
    

    _team_fields.html.erb

    <div class="input">
        <%= f.collection_select(:employee_id, Employee.all, :id, :name, :include_blank => true ) %>
    </div>
    

    The key was adding the <input id="project_teams_none" name="team[employee_ids][]" type="hidden" value="" /> line in manually, because for whatever reason this wasn’t generated as part of the form. This got the form to actually start updating things, and then I just had to make the nested form refer to the join model (team) rather than to employees so that the updates were going to the right place.

    Looks so simple now!

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.