DEV Community

David Paluy
David Paluy

Posted on

1

Rails Migration custom Direction Action

Image description

There are several options to specify direction dependent actions in Rails applications

Example 1 - reversible

class AddSlugToUserss < ActiveRecord::Migration
  def change
    add_column :users, :slug, :string, limit: 64
    add_index :users, :slug, unique: true

    reversible do |dir|
      dir.up do
        User.find_each(&:save)
      end
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Reference: https://apidock.com/rails/ActiveRecord/Migration/reversible

Example 2 - up_only

class AddSlugToUserss < ActiveRecord::Migration
  def change
    add_column :users, :slug, :string, limit: 64
    add_index :users, :slug, unique: true

    up_only do |dir|
      User.find_each(&:save)
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Reference: https://apidock.com/rails/ActiveRecord/Migration/up_only

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay