Rails 7+ framework guardrails, patterns, and best practices for AI-assisted development.
Use when working with Rails projects, or when the user mentions Ruby on Rails.
Provides ActiveRecord, Hotwire/Turbo, Action Cable, and convention-over-configuration guidelines.
Rails 7+ framework guardrails, patterns, and best practices for AI-assisted development.
Use when working with Rails projects, or when the user mentions Ruby on Rails.
Provides ActiveRecord, Hotwire/Turbo, Action Cable, and convention-over-configuration guidelines.
Use t.references for foreign keys (adds index automatically)
Use t.timestamps for created_at and updated_at
Add composite indexes for common query patterns
Use comments_count with counter_cache: true on the association
Service Objects
# app/services/application_service.rbclassApplicationServicedefself.call(...)
new(...).call
endend# app/services/posts/publish_service.rbmodulePostsclassPublishService < ApplicationServicedefinitialize(post:, user:)
@post = post
@user = user
enddefcallreturnServiceResult.failure(["Not authorized"]) unless@user == @post.user
ActiveRecord::Base.transaction do@post.update!(status::published, published_at:Time.current)
notify_subscribers
endServiceResult.success(@post)
rescueActiveRecord::RecordInvalid => e
ServiceResult.failure(e.record.errors.full_messages)
endprivatedefnotify_subscribersPostMailer.published(@post).deliver_later
endendend
Use self.call(...) class method pattern for clean invocation
Wrap multi-step operations in ActiveRecord::Base.transaction
Return a result object (success/failure) instead of raising
Namespace services by domain: Posts::PublishService, Users::CreateService
Rails Commands
# Application
rails new myapp --database=postgresql --css=tailwind
rails new myapp --api # API-only mode
rails server # Start dev server
rails console # Interactive console
rails routes # List all routes# Generators
rails generate model User name:string email:string
rails generate controller Posts index show new create
rails generate scaffold Article title:string body:text
rails generate migration AddStatusToPosts status:integer# Database
rails db:create # Create database
rails db:migrate # Run pending migrations
rails db:rollback # Undo last migration
rails db:seed # Run seeds.rb
rails db:reset # Drop, create, migrate, seed# Testing
rails test# Run all tests
rails test:models # Model tests only
rails test:system # System tests only# Assets and dependencies
bundle install # Install gems
rails assets:precompile # Compile assets for production
Testing
Minitest (Default)
# test/models/post_test.rbrequire"test_helper"classPostTest < ActiveSupport::TestCasedefsetup@post = posts(:first_post)
end
test "valid post"do
assert @post.valid?
end
test "invalid without title"do@post.title = nil
assert_not @post.valid?
assert_includes @post.errors[:title], "can't be blank"end
test "publish! sets status and timestamp"do@post.publish!
assert @post.published?
assert_not_nil @post.published_at
endend
Testing Standards
Use fixtures (default) or factory_bot for test data
Test validations, associations, scopes, and instance methods on models
Test authentication, authorization, and response codes on controllers
Use system tests (Capybara) for critical user flows
Coverage target: >80% for models and services, >60% overall
Test names describe behavior: test "user cannot edit others' posts"