Rails migrations: self.up and self.down versus change

migration, rails-activerecord, rails-migrations, ruby-on-rails

Solution

For many operations rails can guess what is the inverse operation (without problems). For example, in your case what is the reverse operation of `add_column` to call when you rollback? Of course it's `remove_column`. What is the inverse of `create_table`? It's `drop_table`. So in these cases rails know how to rollback and define a `down` method is superfluous (you can see in the documentation the methods currently supported from the change method).

But pay attention because for some kind of operation you still need to define the `down` method, for example if you change the precision of a decimal column how to guess the original precision on rollback? It's not possible, so you need to define the `down` method.

As said, I suggest you to read the Rails Migrations Guide.

Problem

Looks like the new rails version has "change" versus self.up and self.down methods. So what happens when one has to roll back a migration how does it know what actions to perform. I have the following method that I need to implement based on an online tutorial: ``` class AddImageToUsers < ActiveRecord::Migration def self.up add_column :users, :image_file_name, :string add_column :users, :image_content_type, :string add_column :users, :image_file_size, :integer add_column :users, :image_updated_at, :datetime end def self.down remove_column :users, :image_file_name, :string remove_column :users, :image_content_type, :string remove_column :users, :image_file_size, :integer remove_column :users, :image_updated_at, :datetime end end ``` How can I do the same using the new change method?

Original source