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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)