rails-state-machines

Automate Rails model state machines with explicit transitions and guards.

4|Updated Feb 16, 2017
One-click install
npx skills add https://github.com/nekorush14/dotfiles --skill rails-state-machines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-state-machines
Source: https://github.com/nekorush14/dotfiles/tree/main/configs/claude/skills/rails-state-machines
Command: npx skills add https://github.com/nekorush14/dotfiles --skill rails-state-machines

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill demonstrates implementing state machines (via AASM) to manage complex workflows and state transitions in Rails models.

Core Features & Use Cases

  • Explicit States & Transitions: Clearly defined states with guarded transitions.
  • Callbacks on State Changes: Hooks for side effects during state changes.
  • Single Source of Truth: State persisted in the database with consistent behavior.

Quick Start

Implement an Order model with states: pending -> confirmed -> shipped -> delivered and an optional cancel path.

Frequently Asked Questions about rails-state-machines

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement state machines in Rails models?

State machines in Rails models use AASM to define explicit states, transitions, and callbacks. Define states like pending → confirmed → shipped, add guard conditions to control which transitions are allowed, and persist the current state in the database as a single source of truth for your workflow.

What are callbacks in Rails state transitions and when do I use them?

Callbacks in state transitions are hooks that execute side effects when a model changes state. Use them to trigger notifications, update related records, or log events—for example, sending a confirmation email when an order transitions from pending to confirmed.

Can I add guards to prevent invalid state transitions in Rails?

Yes, guarded transitions in Rails state machines let you define conditions that must be met before allowing a state change. Guards enforce business rules—for example, preventing an order from shipping unless payment is confirmed—ensuring only valid transitions occur.

How do I model complex workflows like order lifecycles with state machines?

Model order lifecycles by mapping real-world stages to explicit states (pending, confirmed, shipped, delivered) and defining allowed transitions between them. Include optional paths like cancellation, and use callbacks to handle side effects at each stage, keeping all state logic in one place.

Why use a state machine instead of storing workflow state as free text?

State machines enforce valid transitions, prevent invalid states, and centralize business logic in one source of truth persisted in the database. This eliminates silent bugs from typos or inconsistent state values and makes workflows testable and auditable.

What scenarios work best with state machines in Rails applications?

State machines excel for order processing, user account provisioning, document approval flows, and payment processing—any workflow with distinct stages, conditional transitions, and side effects tied to state changes.