class MoveSpeciesToRevision < ActiveRecord::Migration[5.1] def up change_table :revisions do |t| t.references :species, null: true end Pokeviewer::Revision.all.each do |r| r.species_id = r.pokemon.species_id r.save! end remove_column :pokemon, :species_id change_column_null :revisions, :species_id, false add_foreign_key :revisions, :species, column: :species_id end def down def up change_table :pokemon do |t| t.references :species, null: true end Pokeviewer::Pokemon.all.each do |p| p.species_id = p.revisions.first.species_id p.save! end remove_column :revisions, :species_id change_column_null :pokemon, :species_id, false add_foreign_key :pokemon, :species, column: :species_id end end end