title: How do I add and remove multiple "belongs_to" instances to and from a "has_many" instance?
body: <p>I currently have two models: Campaigns and Videos. Videos belongs to Campaigns and a Campaign has many Videos. In my Campaign form I want to be able to add Videos that have no parent and also be able to remove Videos that belong to the selected campaign. I came up with using two separate multiple selection lists for this. One list has all orphaned Videos and the other has all videos that belong to the selected campaign. That way a user and just select which videos to add and remove. I've run into trouble when trying to create the logic for adding and removing Videos from the selected Campaign in my "update" and "create" methods. I imagine that somehow I would need to take an array from each the selection lists and run a loop that adds and a loop that removes the selected videos in each form. </p>
  
  <p>I'll post what I have so far from my form and my controllers:</p>
  
  <p>Campaigns Controller - Update method:</p>
  
  <pre><code>  def update
      if @campaign.update_attributes(params[:campaign])
        unless request.xhr?
          flash[:notice] = "'#{@campaign.title}' was successfully updated."
        else
          flash.now[:notice] = "'#{@campaign.title}' was successfully updated."
        end
        unless from_dialog?
          unless params[:continue_editing] =~ /true|on|1/
            redirect_to admin_campaigns_url
          else
            unless request.xhr?
              redirect_to :back
            else
              render :partial =&gt; "/shared/message"
            end
          end
        else
          render :text =&gt; "&lt;script type='text/javascript'&gt;parent.window.location = '\#{admin_campaigns_url}';&lt;/script&gt;"
        end
      else
        unless request.xhr?
          render :action =&gt; 'edit'
        else
          render :partial =&gt; "/shared/admin/error_messages_for", :locals =&gt; {:symbol =&gt; :campaign, :object =&gt; @campaign}
        end
      end
    end
  </code></pre>
  
  <p>Campaign Form Partial:</p>
  
  <pre><code>&lt;%= error_messages_for :campaign -%&gt;
  &lt;% form_for [:admin, @campaign] do |f| -%&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :title -%&gt;
      &lt;%= f.text_field :title, :class =&gt; 'larger' -%&gt;
    &lt;/div&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :description -%&gt;
      &lt;%= f.text_area :description, :rows =&gt; 20, :cols =&gt; 140, :class =&gt; 'wymeditor' -%&gt;
    &lt;/div&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :date -%&gt;
      &lt;%= f.date_select :date -%&gt;
    &lt;/div&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :videos_in, "Add Videos" -%&gt;
      &lt;%= f.collection_select(:title, @orphanedVideos, :id, :title, {}, {:multiple =&gt; true}) -%&gt;
    &lt;/div&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :videos_out, "Remove Videos" -%&gt;
      &lt;%= f.collection_select(:title, @campaignVideos, :id, :title, {}, {:multiple =&gt; true}) -%&gt;
    &lt;/div&gt;
  
    &lt;div class='field'&gt;
      &lt;%= f.label :preview -%&gt;
      &lt;%= render :partial =&gt; "/shared/admin/image_picker", :locals =&gt; {
        :f =&gt; f,
        :field =&gt; :preview_id,
        :image =&gt; @campaign.preview,
        :toggle_image_display =&gt; false
      } %&gt;
    &lt;/div&gt;
  
    &lt;%= render :partial =&gt; "/shared/admin/form_actions", :locals =&gt; {:f =&gt; f, :continue_editing =&gt; false} %&gt;
  &lt;% end -%&gt;
  </code></pre>
  
  <p>I'm not sure if the collection_select's are setup properly (though they do display correctly on the form). Any pointers would be appreciated. </p>
  
  <p>Thanks for looking!</p>