title: How can I move data from one table to another in rails migration?
body: <p>I have set up a rails site with these resources:</p>
  
  <p>users(devise)
  authors</p>
  
  <p>I have realised that I need to move all authors over to users table because I am setting up roles for these users as an author now.</p>
  
  <p>Currently I have tried the following migration but nothing worked and no data was transferred over to the users table:</p>
  
  <pre><code>class MoveAuthorsColumnDataToUsersTable &lt; ActiveRecord::Migration
    def change
      Author.find_each do |author|
         User.find_or_create_by(
             :username =&gt; author.name.downcase.gsub(' ',''),
             :encrypted_password =&gt; "",
             :email =&gt; "",
             :avatar_file_name =&gt; author.avatar_updated_at,
             :avatar_content_type =&gt; author.avatar_content_type,
             :avatar_file_size =&gt; author.avatar_file_size,
             :avatar_updated_at =&gt; author.avatar_updated_at,
             :role_id =&gt; "3"
         )
       end
    end
  end
  </code></pre>
  
  <p>I have got an author model setup with attributes in the controller, I am eventually going to transfer all author models relationships to users.</p>
  
  <p>author.rb model:</p>
  
  <pre><code>class Author &lt; ActiveRecord::Base
      has_many :books, dependent: :destroy
      has_many :ideas, dependent: :destroy
      accepts_nested_attributes_for :books
      accepts_nested_attributes_for :ideas
      validates_uniqueness_of :name
  
      has_attached_file :avatar, :styles =&gt; { :large =&gt; "980x760", :medium =&gt; "300x300&gt;", :thumb =&gt; "100x100&gt;" }, :default_url =&gt; "/images/:style/missing.png"
      validates_attachment_content_type :avatar, :content_type =&gt; /\Aimage\/.*\Z/
  
  end
  </code></pre>
  
  <p>user.rb model(devise)</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    belongs_to :role
    def confirmation_required?
      false
    end
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :confirmable
  
  
    has_attached_file :avatar, styles: {
          large: "600x450#",
          medium: "250x250#",
          small: "100x100#"
      }, :default_url =&gt; "/images/:style/filler.png"
    #validates_attachment_content_type :avatar, :content_type =&gt; ["image/jpg", "image/jpeg", "image/png", "image/gif"]
    validates_attachment_content_type :avatar, :content_type =&gt; /\Aimage\/.*\Z/
    validates :avatar, :email, :username, :password, presence: true
  
  end
  </code></pre>
  
  <p>Here is the schema for users and authors:</p>
  
  <pre><code>  create_table "authors", force: true do |t|
      t.string   "name"
      t.text     "biography"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.integer  "book_id"
      t.string   "avatar_file_name"
      t.string   "avatar_content_type"
      t.integer  "avatar_file_size"
      t.datetime "avatar_updated_at"
      t.integer  "idea_id"
    end
  
    create_table "users", force: true do |t|
      t.string   "email",                  default: "", null: false
      t.string   "encrypted_password",     default: "", null: false
      t.string   "reset_password_token"
      t.datetime "reset_password_sent_at"
      t.datetime "remember_created_at"
      t.integer  "sign_in_count",          default: 0,  null: false
      t.datetime "current_sign_in_at"
      t.datetime "last_sign_in_at"
      t.string   "current_sign_in_ip"
      t.string   "last_sign_in_ip"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.string   "avatar_file_name"
      t.string   "avatar_content_type"
      t.integer  "avatar_file_size"
      t.datetime "avatar_updated_at"
      t.string   "username"
      t.string   "confirmation_token"
      t.datetime "confirmed_at"
      t.datetime "confirmation_sent_at"
      t.integer  "role_id"
    end
  </code></pre>
  
  <p>The end result I am trying to achieve here is to get all authors safely over to users as entries in the postgres database and then from there I can use this data to carry on finishing off what else needs refactoring in the models for users etc.</p>
  
  <p>This will hopefully help someone try and find the right solution to what i am trying to do. I am pretty sure many people have been here before and had the same headache.</p>
  
  <p>Thanks</p>