title: Rails remove duplicated associated records
body: <p>Let's say I have a <code>User</code> and User <code>has_many :tags</code> and I would like to remove all <code>@users</code> tags that have duplicated <code>name</code>. For example,</p>
  
  <pre><code>@user.tags #=&gt; [&lt;Tag name: 'A'&gt;, &lt;Tag name: 'A'&gt;, &lt;Tag name: 'B'&gt;]
  </code></pre>
  
  <p>I would like to keep only the tags with unique names and delete the rest from the database.</p>
  
  <p>I know I could pull out a list of unique tags names from user's tags and remove all users's tags and re-create user's tags with only unique names but it would be ineffficient?</p>
  
  <p>On the other hand, <code>select</code> won't work as it returns only the selected column. <code>uniq</code> also won't work:</p>
  
  <pre><code>@user.tags.uniq #=&gt; returns all tags
  </code></pre>
  
  <p>Is there a more efficient way?</p>
  
  <p>UPDATE:
  I would like to do this in a migration.</p>