instance method
reversible
Ruby on Rails 8.0.4
Since v4.0.13Signature
reversible()
Used to specify an operation that can be run in one direction or another. Call the methods up and down of the yielded object to run a block only in one given direction. The whole block will be called in the right order within the migration.
In the following example, the looping on users will always be done when the three columns ‘first_name’, ‘last_name’ and ‘full_name’ exist, even when migrating down:
class SplitNameMigration < ActiveRecord::Migration[8.0]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
reversible do |dir|
User.reset_column_information
User.all.each do |u|
dir.up { u.first_name, u.last_name = u.full_name.split(' ') }
dir.down { u.full_name = "#{u.first_name} #{u.last_name}" }
u.save
end
end
revert { add_column :users, :full_name, :string }
end
end
Source
# File activerecord/lib/active_record/migration.rb, line 908
def reversible
helper = ReversibleBlockHelper.new(reverting?)
execute_block { yield helper }
end
Defined in activerecord/lib/active_record/migration.rb line 908
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Migration