title: How to migrate a existing one-to-many relationship to many-to-many in Rails and ActiveRecord
body: <p>I have a model A,</p>
  
  <pre><code>Class A &lt; ActiveRecord::Base
      has_many: names, class_name: 'B'
  </code></pre>
  
  <p>and a model B</p>
  
  <pre><code>class B &lt; ActiveRecord::Base
      belongs to :A
  </code></pre>
  
  <p>and there are already a bunch of data in database.</p>
  
  <p>How do I write a migration to migrate them from one-to-many to many-to-many relationship? I prefer to use </p>
  
  <pre><code>has_many: through
  </code></pre>
  
  <p>if possible.</p>
  
  <p>It's not hard to write the db migration, but what do I do to migrate the data in it?</p>