Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1rake

Rails Migration - what's the point of self.up and self.down


I was wondering what those meant:

class ChangeTimeColumns < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end

as sometimes Rails just creates a migration like this:

class ChangeTimeColumns < ActiveRecord::Migration
  def change
  end
end

Would appreciate any clarification.


Solution

  • It is so you can reverse the migration if you have made a mistake.

    rake db:rollback   #this will rollback the most recent migration
    
    
    rake db:rollback STEP=3  #this will rollback 3 of them
    

    I add self.up and self.down to all my migrations, and if there is a migration that is not possible to reverse, you can not include down or raise an exception in down. Check out section 4.1 in this guide ====> http://guides.rubyonrails.org/migrations.html#using-the-up-down-methods