Migration Methods in Ruby

From the Ruby on Rails cheat sheet · Database & Migrations · verified Jul 2026

Migration Methods

Common methods used in migration files

ruby
class CreateProducts < ActiveRecord::Migration[7.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
      t.decimal :price, precision: 8, scale: 2
      t.integer :stock, default: 0
      t.references :category, foreign_key: true

      t.timestamps
    end

    add_index :products, :name
  end
end
✅ t.timestamps adds created_at and updated_at
💡 Use references for foreign keys with index
🔍 precision and scale control decimal places
⚡ Add indexes for frequently queried columns
migrationsschemadatabase

Continue with Ruby on Rails

Save the full cheat sheet or work through every related task.

More Ruby tasks

Back to the full Ruby on Rails cheat sheet