title: Updating a Many-to-many relationship with additional field by form_for
body: <p>Is there a way to update a field relative to a many-to-many associations via form_for?</p>
  
  <p>Theses are my models:</p>
  
  <pre><code>  class Grad &lt; ActiveRecord::Base
      belongs_to :school
      has_many :grad_courses
      has_many :courses, through: :grad_courses
      accepts_nested_attributes_for :grad_courses, :courses
    end
  
    class Course &lt; ActiveRecord::Base
      belongs_to :school
      has_many :grad_courses
      has_many :grads, through: :grad_courses
    end
  
    class GradCourse &lt; ActiveRecord::Base
      belongs_to :grad
      belongs_to :course
    end
  </code></pre>
  
  <p>Here is the many-to-many migration with the extra property (semester):</p>
  
  <pre><code>  create_table "grad_courses", force: :cascade do |t|
      t.integer  "grad_id"
      t.integer  "course_id"
      t.integer  "semester"
      t.datetime "created_at"
      t.datetime "updated_at"
    end
  </code></pre>
  
  <p>What i'm trying to do is add and update the relations with the semester information dynamically with a form collection set (using form_for and chosen).</p>
  
  <pre><code>&lt;%= form_for @grad, html: {class: "form-horizontal"} do |f| %&gt;
      ...    
         &lt;% (1..@grad.max).each do |semester| %&gt;
              &lt;div class="form-group"&gt;
                  &lt;%= f.label :course_ids, "#{semester}º semestre", class: "col-sm-2 control-label"  %&gt;
                  &lt;div class="col-sm-10"&gt;
                    &lt;%= f.collection_select :course_ids, @courses, :id, :name, {include_blank: true}, {multiple: true, :class=&gt;'chosen-select form-control'} %&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;% end %&gt;  
  ...   
      &lt;% end %&gt;
  </code></pre>
  
  <p>This way, it is creating and updating the grad_courses, but without the semester.</p>
  
  <p>I've tried lot of things, but didn't had success. </p>
  
  <p>I'm able to manage the semester's info directly, like in this post: <a href="https://stackoverflow.com/questions/2168442/many-to-many-relationship-with-the-same-model-in-rails#_=_">http://stackoverflow.com/questions/2168442/many-to-many-relationship-with-the-same-model-in-rails#<em>=</em></a></p>
  
  <p>I could pass an array with a hidden field, and manage the form manually, but it would take too much work, so I was wondering if there is an easy way to do this.</p>
  
  <p>Something like :grad_course[:course_id][:semester] in :grad params? Is it possible?</p>
  
  <p>the grad_course_controller:</p>
  
  <pre><code>      def update
          respond_to do |format|
            if @grad.update(grad_params)
              format.html { redirect_to @grad, notice: 'Cursos adicionados com sucesso' }
            else
              format.html { render :edit }
            end
          end
        end
  
        private
          def set_grad
            @grad = Grad.find(params[:id])
          end
  
          def set_courses
             @courses = Course.where(school_id: @grad.school_id) 
             @gc = GradCourse.where(grad_id: @grad.id)
          end
  list through.
          def grad_params
            params.require(:grad).permit(:name, :min, :max, :school_id, course_ids: [] )
          end
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I've been trying to solve this using Peter's advices, so I tried nested_attributes and fields_for. But I'm having problems with it too.</p>
  
  <p>I need one collection_sellect for each semester (from 1 to Grad.max number), where the options will be the courses available for that semester (@courses), and for the selected ones a grad_course relation needs to be created or updated.</p>
  
  <p>The problem with field_for is that one field is generated for every grad_course relation, and since each field is related with the grad_course.id I`m not able to add new ones properly.</p>
  
  <p>Here is one of the tries i've made with fields for:</p>
  
  <pre><code>  &lt;%= f.fields_for :grad_courses do |builder| %&gt;
    &lt;p&gt;
      &lt;%= builder.text_field :semester %&gt;&lt;br /&gt;
      &lt;%= builder.collection_select :course_id, @courses, :id, :name, {include_blank: true}, {multiple: true, :class=&gt;'chosen-select form-control'} %&gt;
  
    &lt;/p&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p>with these changes in the grad_controller:</p>
  
  <pre><code>def grad_params
    params.require(:grad).permit(:name, :min, :max, :school_id, course_ids: [], grad_courses_attributes: [:id, :course_id, :grad_id, :semester ])
  end
  </code></pre>