Rinvert

March 17, 2012

In case you’re not aware, rails.vim is great.  One of the commands that I just found out about is Rinvert.  If you’re working on a migration such as:

class AddAwesomeColumnsToAwesomeTable < ActiveRecord::Migration
  def change
    add_column :awesome_people, :awesome_level, :integer
    add_column :awesome_people, :awesome_explanation, :string
  end
  
  def down
  end
end

and you get to the down migration, you’re welcome to begin typing out the commands to drop the columns, however if you simply highlight the up method and call:

:Rinvert

all the magic will be done for you!  Rails.vim will read what you’re doing and add the appropriate drops.  If the migration is too complicated for it, it will add what it can and then mark it with an irreversible migration exception which then you can take out and change the more complicated stuff by hand.

Please note, in the newer versions of rails you can simply use change instead of up and down but there are times this won’t work.  Even when it does, legacy code unfortunately still exists so this command can come in very handy.

Discussion, links, and tweets

I'm a developer at thoughtbot.

Follow me on Twitter. I rarely post anything but it can't hurt to try.