Ruby rails

From erikstmartin/dotfiles · View interactive version

Ruby rails is an AI agent skill from the GitHub repository erikstmartin/dotfiles. Use for Rails apps: ActiveRecord/models, migrations, controllers/routes, background jobs, caching, and Hotwire. Not for non-Rails Ruby.

How to use this skill

Skill file

Ruby on Rails

Idiomatic Rails development — conventions, ActiveRecord, and production patterns.

Workflow

  1. Model — schema + migrations + validations + scopes
  2. Controller — thin controllers, strong params, respond_to
  3. Background — ActiveJob with Sidekiq, idempotent + retry-safe
  4. Performance — N+1 prevention, caching, counter caches
  5. Test — RSpec/Minitest with FactoryBot, request specs for behavior

Migration Example

class AddEmailVerifiedToUsers < ActiveRecord::Migration[7.1]
  def change
    # Step 1: nullable column (safe to deploy before backfill)
    add_column :users, :email_verified, :boolean

    # Step 2: backfill in batches to avoid full-table lock
    User.in_batches.update_all(email_verified: false)

    # Step 3: enforce the constraint after backfill completes
    change_column_null :users, :email_verified, false, false
    change_column_default :users, :email_verified, false
  end
end

Always add indexes alongside foreign keys and query predicates. Use add_index :orders, :customer_id in the same migration as the column.

ActiveRecord Patterns

class Order < ApplicationRecord
  belongs_to :customer
  has_many :items, dependent: :destroy

  # Composable scopes — chain freely without N+1
  scope :recent,        -> { order(created_at: :desc) }
  scope :for_customer,  ->(id) { where(customer_id: id) }
  scope :pending,       -> { where(status: :pending) }
  scope :with_details,  -> { includes(:items, :customer) }

  # Enum for finite states — generates predicates like order.pending?
  enum :status, { pending: 0, confirmed: 1, shipped: 2, delivered: 3 }

  validates :customer, presence: true
  validates :status, presence: true
end

Controller stays thin — it composes scopes, it doesn't implement business logic:

class OrdersController < ApplicationController
  def index
    @orders = Order.with_details.recent.for_customer(params[:customer_id])
  end

  def create
    @order = Order.new(order_params)
    if @order.save
      redirect_to @order, notice: "Order created"
    else
      render :new, status: :unprocessable_entity
    end
  end

  private

  def order_params
    params.require(:order).permit(:customer_id, :notes, items_attributes: [:product_id, :quantity])
  end
end

Background Job Pattern

class ProcessOrderJob < ApplicationJob
  queue_as :default
  retry_on ActiveRecord::Deadlocked, wait: 5.seconds, attempts: 3
  discard_on ActiveRecord::RecordNotFound

  def perform(order_id)
    order = Order.find_by(id: order_id)
    return unless order&.pending?  # Idempotent guard — safe to re-run

    ActiveRecord::Base.transaction do
      order.confirm!
      InventoryService.reserve(order)
    end

    OrderMailer.confirmation(order).deliver_later
  end
end

Pass IDs, not full objects — Active Job serializes records by ID and re-loads on execution to avoid stale data.

Caching

# Fragment caching with automatic expiry via cache_key_with_version
<% cache @order do %>
  <%= render @order %>
<% end %>

# Russian-doll: parent key changes when child changes
<% cache [@customer, @customer.orders.maximum(:updated_at)] do %>
  <% @customer.orders.each do |order| %>
    <% cache order do %><%= render order %><% end %>
  <% end %>
<% end %>

For low-traffic read-heavy data, use Rails.cache.fetch:

def self.featured
  Rails.cache.fetch("products/featured", expires_in: 1.hour) do
    where(featured: true).with_details.to_a
  end
end

Pitfalls

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.