title: Rails 4 nested attributes and has_many :through associaton in a form
body: <p>I am having an issue managing a has_many :through association using a form. What I DON'T want to do is edit the attributes of the associated model of which there is a plethora of information out there, rather, I want to manage the association ONLY. I understand I could do this by manipulating the form parameters passed to my action and build the relationships manually, but I would prefer to go the Rails way, if that is possible.</p>
  
  <p>One interesting thing about my case is that this has_many :through association is actually on a Model which I am already saving using accepts_nested_attributes_for</p>
  
  <p>Here are my models: Goals, Milestones and Programs.</p>
  
  <pre><code>class Goal &lt; ActiveRecord::Base
    has_many :milestones, inverse_of: :goal, dependent: :destroy
    accepts_nested_attributes_for :milestones, :allow_destroy =&gt; true
  end
  
  class Milestone &lt; ActiveRecord::Base
    belongs_to :goal, inverse_of: :milestones
  
    has_many :milestone_programs
    has_many :programs, :through =&gt; :milestone_programs
  end
  
  class Program &lt; ActiveRecord::Base
  end
  </code></pre>
  
  <p>Now in my Goal edit view, I need to be able to add and remove milestones, and for those milestones I need to be able to add and remove program associations. This is the code for my form.</p>
  
  <pre><code>&lt;%= form_for @goal do |f| %&gt;
  
    &lt;%= f.fields_for :milestones do |f_milestone| %&gt;
  
      &lt;%= f.hidden_field :id, :value =&gt; f.object.id %&gt;
      &lt;%= f.hidden_field :name, :value =&gt; f.object.name %&gt;
      &lt;a href="javascript:void(0)" class="milestone-remove"&gt;- remove&lt;/a&gt;
  
      &lt;ul&gt;
        &lt;%= f.fields_for :programs do |f_prog| %&gt;
          &lt;li&gt;
            &lt;%= f_prog.object.name %&gt;
            &lt;a href="javascript:void(0)" class="program-remove"&gt;- remove&lt;/a&gt;
          &lt;/li&gt;
        &lt;% end %&gt;
      &lt;/ul&gt;
  
    &lt;% end %&gt;
  
    &lt;%= f.submit 'Save' %&gt;
  
  &lt;% end %&gt;
  </code></pre>
  
  <p>In my controller, I have</p>
  
  <pre><code>class GoalsController &lt; ApplicationController
  
      # PATCH/PUT /goals/:id
      def update
        if @goal.update(goal_params)
          redirect_to @goal
        end
      end
  
      def goal_params
        params.require(:goal).permit(:name, :milestones_attributes =&gt; [ :id, :name, :_destroy ])
      end
  
  end
  </code></pre>
  
  <p>This form needs to be like a worksheet where you can make changes and only save your changes once you click save at the end, so I don't believe gems such as cocoon or nested_forms will help.</p>
  
  <p>My code works perfectly so far for managing my Goals associated Milestones and their attributes. But now I want to be able to manage the list of Programs associated to those Milestones.</p>
  
  <p>I have tried using accepts_nested_attributes_for but that is not exactly what I want because I don't care to edit the nested attributes of the model, the Program attributes are to remain static.</p>
  
  <p>I thought I might be able to have something like this in my form for each program to build the associations:</p>
  
  <pre><code>&lt;input type="hidden" name="goal[milestones_attributes][1][program_ids][1]" &gt;
  </code></pre>
  
  <p>But that doesn't work either (of course I've added :program_ids to my white-listed parameters). Is there a magic rails method I need to add to my controller?</p>
  
  <p>What am I missing here?</p>
  
  <p>Thanks in advance!</p>