Reversible migrations in Rails 3.1+

April 26, 2012 Mark Rushakoff

In Rails 3.1 and newer, when you write a migration by hand, you can (usually) just define a change method instead of an up and a down method.

This StackOverflow answer from user Readonly demonstrates the use of change nicely:

class RenameOldTableToNewTable< ActiveRecord:Migration
  def self.up
    rename_table :old_table_name, :new_table_name
  end
  def self.down
    rename_table :new_table_name, :old_table_name
  end
end

becomes

class RenameOldTableToNewTable< ActiveRecord:Migration
  def change
     rename_table :old_table_name, :new_table_name
  end
end

About the Author

Biography

More Content by Mark Rushakoff
Previous
Cloud Foundry Powers Data Sets for Mumbai
Cloud Foundry Powers Data Sets for Mumbai

This is the third post of a series of guest blogs by application developers. We are featuring a use case by...

Next
iOS Roundup: April 16, 2012
iOS Roundup: April 16, 2012

Cool Things This Week: We have confirmed (using instruments) that having a large sized (full-screen iPad) ...