title: Implement a Rails 3 nested form with has-many through association
body: <p>I'm trying to implement a Rails3 nested form with has-many through association. </p>
  
  <p>My model relationships are as follows (My models are Project ProjectDuration, ProjectFee). Project can have many project duration through project_fees.</p>
  
  <p>Following are my models/table</p>
  
  <pre><code>mysql&gt; desc projects;
  +---------------+--------------+------+-----+---------+----------------+
  | Field         | Type         | Null | Key | Default | Extra          |
  +---------------+--------------+------+-----+---------+----------------+
  | id            | int(11)      | NO   | PRI | NULL    | auto_increment |
  | title         | varchar(255) | YES  |     | NULL    |                |
  +---------------+--------------+------+-----+---------+----------------+
  
  class Project &lt; ActiveRecord::Base
    has_many :project_durations, :through =&gt; :project_fees
    has_many :project_fees
    accepts_nested_attributes_for :project_fees
  end
  
  
  mysql&gt; desc project_durations;
  +----------+--------------+------+-----+---------+----------------+
  | Field    | Type         | Null | Key | Default | Extra          |
  +----------+--------------+------+-----+---------+----------------+
  | id       | int(11)      | NO   | PRI | NULL    | auto_increment |
  | duration | varchar(255) | YES  |     | NULL    |                |
  +----------+--------------+------+-----+---------+----------------+
  
  class ProjectDuration &lt; ActiveRecord::Base
    has_many :projects, :through =&gt; :project_fees
    has_many :project_fees
  end
  
  
  mysql&gt; desc project_fees;
  +---------------------+----------+------+-----+---------+----------------+
  | Field               | Type     | Null | Key | Default | Extra          |
  +---------------------+----------+------+-----+---------+----------------+
  | id                  | int(11)  | NO   | PRI | NULL    | auto_increment |
  | projec_id           | int(11)  | YES  |     | NULL    |                |
  | project_duration_id | int(11)  | YES  |     | NULL    |                |
  | fee                 | float    | YES  |     | NULL    |                |
  +---------------------+----------+------+-----+---------+----------------+
  
  class ProjectFee &lt; ActiveRecord::Base
    belongs_to :projects
    belongs_to :project_durations
  end
  </code></pre>
  
  <p>And my projects_controllers new action is as follows</p>
  
  <pre><code>class ProjectsController &lt; AdminsController
    def new
      @project = Project.new
      @project_durations = ProjectDuration.find(:all)
      project_fees = @project.project_fees.build()
  
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml =&gt; @project }
      end
    end
  end
  </code></pre>
  
  <p>And I finally I have the following view (<em>new.erb</em>)</p>
  
  <pre><code>&lt;%= form_for(@project, :html =&gt; { :class =&gt; :form }) do |f| -%&gt;
       &lt;% @project_durations.each do |duration| %&gt;
        &lt;%= f.fields_for :project_fees do |builder|  %&gt;
          &lt;%= render 'fee_fields', :f =&gt; builder, :project =&gt; @project, :duration =&gt; duration %&gt;
        &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;% end -%&gt;
  </code></pre>
  
  <p>and 'fee_fields' is as </p>
  
  <pre><code>&lt;ul&gt;
    &lt;li&gt;
      &lt;%= duration.duration %&gt;
      &lt;%= f.text_field :fee %&gt;
      &lt;%= f.hidden_field :project_duration_id, :value =&gt; duration.id %&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Even though this saves data to 'project_fees' table, it does not save data in the <code>project_id</code> field of  the <code>project_fees</code> table.</p>
  
  <p>I'm using Rails 3 with Ruby 1.8.7 on Linux.</p>