Ruby on Rails Project

Language: Ruby October 15, 2025

Instruction file for Ruby on Rails framework with modern best practices.

---
applyTo: "**/*.rb, **/*.erb"
---

# Ruby on Rails Development Instructions (2025)

_Last updated: October 2025_

- Always use the fetch tool to look up the latest Rails documentation: https://guides.rubyonrails.org
- Use Rails 7+ features including Hotwire (Turbo and Stimulus) for modern frontend
- Follow Rails conventions and "Convention over Configuration" principle
- Use Active Record for database operations with proper associations
- Implement service objects or concerns for complex business logic
- Use strong parameters for mass assignment protection

**Core Principles:**

- Follow MVC pattern and RESTful routing conventions
- DRY (Don't Repeat Yourself) - extract reusable code
- Use concerns for shared behavior across models/controllers
- Implement callbacks judiciously (avoid overuse)
- Follow Ruby style guide (use Rubocop for linting)

**Database:**

- Use migrations for all database schema changes
- Create seeds for initial data and fixtures/factories for tests
- Use Active Record associations (has_many, belongs_to, etc.)
- Implement database indexes for frequently queried columns
- Use scopes for reusable query logic

**Security:**

- Use Rails built-in authentication (Devise gem) or Rails 7+ has_secure_password
- Implement authorization with Pundit or CanCanCan
- Use strong parameters to prevent mass assignment vulnerabilities
- Enable CSRF protection (enabled by default)
- Use secure_password and has_secure_token for sensitive data

**Testing:**

- Use RSpec or Minitest for testing
- Write request specs for API endpoints
- Use FactoryBot for test data generation
- Implement system tests with Capybara for end-to-end testing
- Aim for high test coverage with meaningful tests

**Performance:**

- Use eager loading (includes, preload, eager_load) to avoid N+1 queries
- Implement caching with Rails.cache and fragment caching
- Use background jobs with Sidekiq or Delayed Job
- Optimize database queries with indexes and counter caches

**Frontend:**

- Use Hotwire (Turbo Drive, Turbo Frames, Turbo Streams) for SPA-like experience
- Implement Stimulus controllers for JavaScript behavior
- Use ViewComponent for reusable view logic
- Follow BEM or similar methodology for CSS naming

**Summary:**

> For all Rails work, always use the fetch tool to look up the latest documentation from https://guides.rubyonrails.org. Write idiomatic Ruby code following Rails conventions and best practices.

Ruby on Rails Ruby Backend Framework Web Development MVC