Rails Expert
Core Workflow
- Analyze requirements — Identify models, routes, real-time needs, background jobs
- Scaffold resources —
rails generate model User name:string email:string, rails generate controller Users
- Run migrations —
rails db:migrate and verify schema with rails db:schema:dump
- If migration fails: inspect
db/schema.rb for conflicts, rollback with rails db:rollback, fix and retry
- Implement — Write controllers, models, add Hotwire (see Reference Guide below)
- Validate —
bundle exec rspec must pass; bundle exec rubocop for style
- If specs fail: check error output, fix failing examples, re-run with
--format documentation for detail
- If N+1 queries surface during review: add
includes/eager_load (see Common Patterns) and re-run specs
- Optimize — Audit for N+1 queries, add missing indexes, add caching
Reference Guide
Load detailed guidance based on context:
| Topic |
Reference |
Load When |
| Hotwire/Turbo |
references/hotwire-turbo.md |
Turbo Frames, Streams, Stimulus controllers |
| Active Record |
references/active-record.md |
Models, associations, queries, performance |
| Background Jobs |
references/background-jobs.md |
Sidekiq, job design, queues, error handling |
| Testing |
references/rspec-testing.md |
Model/request/system specs, factories |
| API Development |
references/api-development.md |
API-only mode, serialization, authentication |
Common Patterns
N+1 Prevention with includes/eager_load
# BAD — triggers N+1
posts = Post.all
posts.each { |post| puts post.author.name }
# GOOD — eager load association
posts = Post.includes(:author).all
posts.each { |post| puts post.author.name }
# GOOD — eager_load forces a JOIN (useful when filtering on association)
posts = Post.eager_load(:author).where(authors: { verified: true })
Turbo Frame Setup (partial page update)
<%# app/views/posts/index.html.erb %>
<%= turbo_frame_tag "posts" do %>
<%= render @posts %>
<%= link_to "Load More", posts_path(page: @next_page) %>
<% end %>
<%# app/views/posts/_post.html.erb %>
<%= turbo_frame_tag dom_id(post) do %>
<h2><%= post.title %></h2>
<%= link_to "Edit", edit_post_path(post) %>
<% end %>
# app/controllers/posts_controller.rb
def index
@posts = Post.includes(:author).page(params[:page])
@next_page = @posts.next_page
end
Sidekiq Worker Template
# app/jobs/send_welcome_email_job.rb
class SendWelcomeEmailJob < ApplicationJob
queue_as :default
sidekiq_options retry: 3, dead: false
def perform(user_id)
user = User.find(user_id)
UserMailer.welcome(user).deliver_now
rescue ActiveRecord::RecordNotFound => e
Rails.logger.warn("SendWelcomeEmailJob: user #{user_id} not found — #{e.message}")
# Do not re-raise; record is gone, no point retrying
end
end
# Enqueue from controller or model callback
SendWelcomeEmailJob.perform_later(user.id)
Strong Parameters (controller template)
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: %i[show edit update destroy]
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post, notice: "Post created."
else
render :new, status: :unprocessable_entity
end
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body, :published_at)
end
end
Constraints
MUST DO
- Prevent N+1 queries with
includes/eager_load on every collection query involving associations
- Write comprehensive specs targeting >95% coverage
- Use service objects for complex business logic; keep controllers thin
- Add database indexes for every column used in
WHERE, ORDER BY, or JOIN
- Offload slow operations to Sidekiq — never run them synchronously in a request cycle
MUST NOT DO
- Skip migrations for schema changes
- Use raw SQL without sanitization (
sanitize_sql or parameterized queries only)
- Expose internal IDs in URLs without consideration
Output Templates
When implementing Rails features, provide:
- Migration file (if schema changes needed)
- Model file with associations and validations
- Controller with RESTful actions and strong parameters
- View files or Hotwire setup
- Spec files for models and requests
- Brief explanation of architectural decisions
Documentation
1---2name: rails-expert3description: Rails 7+ specialist that optimizes Active Record queries with includes/eager_load, implements Turbo Frames and Turbo Streams for partial page updates, configures Action Cable for WebSocket connections, sets up Sidekiq workers for background job processing, and writes comprehensive RSpec test suites. Use when building Rails 7+ web applications with Hotwire, real-time features, or background job processing. Invoke for Active Record optimization, Turbo Frames/Streams, Action Cable, Sidekiq, RSpec Rails.4license: MIT5compatibility: opencode6metadata:7 source: https://skillmd.com/skills/farmage/rails-expert8 author: https://github.com/Jeffallan9 version: "1.1.0"10 domain: backend11 triggers: Rails, Ruby on Rails, Hotwire, Turbo Frames, Turbo Streams, Action Cable, Active Record, Sidekiq, RSpec Rails12 role: specialist13 scope: implementation14 output-format: code15 related-skills: fullstack-guardian, database-optimizer16---1718# Rails Expert1920## Core Workflow21221. **Analyze requirements** — Identify models, routes, real-time needs, background jobs232. **Scaffold resources** — `rails generate model User name:string email:string`, `rails generate controller Users`243. **Run migrations** — `rails db:migrate` and verify schema with `rails db:schema:dump`25 - If migration fails: inspect `db/schema.rb` for conflicts, rollback with `rails db:rollback`, fix and retry264. **Implement** — Write controllers, models, add Hotwire (see Reference Guide below)275. **Validate** — `bundle exec rspec` must pass; `bundle exec rubocop` for style28 - If specs fail: check error output, fix failing examples, re-run with `--format documentation` for detail29 - If N+1 queries surface during review: add `includes`/`eager_load` (see Common Patterns) and re-run specs306. **Optimize** — Audit for N+1 queries, add missing indexes, add caching3132## Reference Guide3334Load detailed guidance based on context:3536| Topic | Reference | Load When |37|-------|-----------|-----------|38| Hotwire/Turbo | `references/hotwire-turbo.md` | Turbo Frames, Streams, Stimulus controllers |39| Active Record | `references/active-record.md` | Models, associations, queries, performance |40| Background Jobs | `references/background-jobs.md` | Sidekiq, job design, queues, error handling |41| Testing | `references/rspec-testing.md` | Model/request/system specs, factories |42| API Development | `references/api-development.md` | API-only mode, serialization, authentication |4344## Common Patterns4546### N+1 Prevention with includes/eager_load4748```ruby49# BAD — triggers N+150posts = Post.all51posts.each { |post| puts post.author.name }5253# GOOD — eager load association54posts = Post.includes(:author).all55posts.each { |post| puts post.author.name }5657# GOOD — eager_load forces a JOIN (useful when filtering on association)58posts = Post.eager_load(:author).where(authors: { verified: true })59```6061### Turbo Frame Setup (partial page update)6263```erb64<%# app/views/posts/index.html.erb %>65<%= turbo_frame_tag "posts" do %>66 <%= render @posts %>67 <%= link_to "Load More", posts_path(page: @next_page) %>68<% end %>6970<%# app/views/posts/_post.html.erb %>71<%= turbo_frame_tag dom_id(post) do %>72 <h2><%= post.title %></h2>73 <%= link_to "Edit", edit_post_path(post) %>74<% end %>75```7677```ruby78# app/controllers/posts_controller.rb79def index80 @posts = Post.includes(:author).page(params[:page])81 @next_page = @posts.next_page82end83```8485### Sidekiq Worker Template8687```ruby88# app/jobs/send_welcome_email_job.rb89class SendWelcomeEmailJob < ApplicationJob90 queue_as :default91 sidekiq_options retry: 3, dead: false9293 def perform(user_id)94 user = User.find(user_id)95 UserMailer.welcome(user).deliver_now96 rescue ActiveRecord::RecordNotFound => e97 Rails.logger.warn("SendWelcomeEmailJob: user #{user_id} not found — #{e.message}")98 # Do not re-raise; record is gone, no point retrying99 end100end101102# Enqueue from controller or model callback103SendWelcomeEmailJob.perform_later(user.id)104```105106### Strong Parameters (controller template)107108```ruby109# app/controllers/posts_controller.rb110class PostsController < ApplicationController111 before_action :set_post, only: %i[show edit update destroy]112113 def create114 @post = Post.new(post_params)115 if @post.save116 redirect_to @post, notice: "Post created."117 else118 render :new, status: :unprocessable_entity119 end120 end121122 private123124 def set_post125 @post = Post.find(params[:id])126 end127128 def post_params129 params.require(:post).permit(:title, :body, :published_at)130 end131end132```133134## Constraints135136### MUST DO137- Prevent N+1 queries with `includes`/`eager_load` on every collection query involving associations138- Write comprehensive specs targeting >95% coverage139- Use service objects for complex business logic; keep controllers thin140- Add database indexes for every column used in `WHERE`, `ORDER BY`, or `JOIN`141- Offload slow operations to Sidekiq — never run them synchronously in a request cycle142143### MUST NOT DO144- Skip migrations for schema changes145- Use raw SQL without sanitization (`sanitize_sql` or parameterized queries only)146- Expose internal IDs in URLs without consideration147148## Output Templates149150When implementing Rails features, provide:1511. Migration file (if schema changes needed)1522. Model file with associations and validations1533. Controller with RESTful actions and strong parameters1544. View files or Hotwire setup1555. Spec files for models and requests1566. Brief explanation of architectural decisions157158[Documentation](https://jeffallan.github.io/claude-skills/skills/backend/rails-expert/)