title: How can I reduce repetition in this Ruby on Rails code?
body: <p>This is a snippet of code from an update method in my application. The method is POSTed an array of user id's in params[:assigned_ users_ list_ id]</p>
  
  <p>The idea is to synchronise the DB associations entries with the ones that were just submitted, by removing the right ones (those that exist in the DB but not the list) and adding the right ones (vise-versa).</p>
  
  <pre><code>    @list_assigned_users = User.find(:all, :conditions =&gt; { :id =&gt; params[:assigned_users_list_id]})
      @assigned_users_to_remove =  @task.assigned_users - @list_assigned_users
      @assigned_users_to_add =  @list_assigned_users - @task.assigned_users
  
      @assigned_users_to_add.each do |user|
          unless @task.assigned_users.include?(user)
              @task.assigned_users &lt;&lt; user
          end
      end
      @assigned_users_to_remove.each do |user|
          if @task.assigned_users.include?(user)
              @task.assigned_users.delete user
          end
      end
  </code></pre>
  
  <p>It works - great!</p>
  
  <p>My first questions is, are those 'if' and 'unless' statements <strong><em>totally</em></strong> redundant, or is it prudent to leave them in place?</p>
  
  <p>My next question is, I want to repeat this <em>exact</em> code immediately after this, but with 'subscribed' in place of 'assigned'... To achieve this I just did a find &amp; replace in my text editor, leaving me with almost this code in my app twice. That's hardly in keeping with the DRY principal!</p>
  
  <p>Just to be clear, every instance of the letters 'assigned' becomes 'subscribed'. It is passed params[:subscribed_ users_ list_ id], and uses @task.subscribed_ users.delete user etc...</p>
  
  <p>How can I repeat this code without repeating it?</p>
  
  <p>Thanks as usual</p>