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?
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_columnto call when you rollback? Of course it’sremove_column. What is the inverse ofcreate_table? It’sdrop_table. So in these cases rails know how to rollback and define adownmethod 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
downmethod, 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 thedownmethod.As said, I suggest you to read the Rails Migrations Guide.