unrename and unrename_column for ActiveRecord

Instead of flipping values around by hand and possibly making mistakes, simply put an ‘un’ before them

Usage

class RenameStuff < ActiveRecord::Migration
  def self.up
    rename_column :users, :username, :login
    rename_column :users, :e_mail, :email
  end

  def self.down
    # simply cody and add 'un'
    unrename_column :users, :username, :login
    unrename_column :users, :e_mail, :email
  end
end

Code

# convenience method, so that rename statements do not need to be reversed by hand
class ActiveRecord::ConnectionAdapters::Table
  def unrename(a,b)
    rename b,a
  end
end
module ActiveRecord::ConnectionAdapters::SchemaStatements
  def unrename_column(table_name, a, b)
    rename_column(table_name, b, a)
  end
end

2 thoughts on “unrename and unrename_column for ActiveRecord

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s