💡 TIL: Rails migrations can include an up_only part
29th October 2024
When writing a migration in Rails and you need to do one thing which can’t be automatically reversed, I always convert a def change to up and down. It turns out you can slip in an up_only block:
class AddFieldsToUsers < ActiveRecord::Migration[7.1]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
up_only do
User.delete_all
end
end
end
from RubyCademy
More recent articles
- Setting up Android phone to work with Apple Watch and iMessage - 11th June 2025
- with_defaults is a better named alias - 28th April 2025
- Bundler can be configured to auto install - 28th April 2025