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
Kaleem Ullah Sep 28 '15 at 10:24 2015-09-28 10:24
source share