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

It seems that the new version of the rails has a β€œchange” compared to the self.up and self.down methods.

So, what happens when you need to cancel the 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 with the new change method?

+69
ruby-on-rails migration
Apr 28 '12 at 15:53
source share
3 answers

For many operations, rails can guess what the reverse operation is (no problem). For example, in your case, what is the inverse of the add_column operation to call on rollback? Of course, this is remove_column . What is the opposite of create_table ? This is drop_table . Therefore, in these cases, the rails can roll back and determine the down method is redundant (you can see in the documentation the methods that are currently supported by the change method ).

But pay attention, because for some operation you still need to define the down method, for example, if you change the precision of the decimal column, how can you guess the original precision when rolling back? This is not possible, so you need to define a down method.

As I said, I suggest you read the Rails Migration Guide .

+93
Apr 28 2018-12-12T00:
source share

Better to use Up, Down, Change:

In Rails 3 (reverse): which should add a new column up and fill all the records in the table only up and delete this column only down

 def up add_column :users, :location, :string User.update_all(location: 'Minsk') end def down remove_column :users, :location end 

But:

You had to avoid using the change method, which allows you to save some time. For example, if you do not need to update the column value immediately after adding it, you would reduce this code to the following:

 def change add_column :users, :location, :string end 

At the bottom, it will add a column to the table and delete it down. Much less code and its profit.

In Rails 4: Another useful way to write what we need in one place:

 def change add_column :users, :location, :string reversible do |direction| direction.up { User.update_all(location: 'Minsk') } end end 
+22
Sep 28 '15 at 10:24
source share
 class AddImageToUsers < ActiveRecord::Migration def change 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 end 
+1
Apr 28 '12 at 15:58
source share



All Articles