method
reversible
Ruby on Rails latest stable (v7.1.3.2)
-
0 notes -
Class: ActiveRecord::Migration
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0
- 3.0.9
- 3.1.0
- 3.2.1
- 3.2.8
- 3.2.13
- 4.0.2 (0)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (0)
- 7.1.3.4 (0)
- What's this?
reversible()
public
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[7.1] 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