Cursor Rules for
Ruby on Rails

This rule explains Ruby on Rails conventions and best practices for backend development.
Back to rules
Type
Backend
Language(s)
Ruby
Stats
434 views
16 copies
13 downloads
ruby-on-rails.mdc
---
description: This rule explains Ruby on Rails conventions and best practices for backend development.
globs: **/*.rb
alwaysApply: false
---

# Ruby on Rails rules

- Use Rails generators to create models, controllers, and migrations.
- Follow Rails naming conventions (singular for models, plural for controllers).
- Use ActiveRecord methods instead of raw SQL queries when possible.
- Avoid N+1 queries by using eager loading with `includes`, `preload`, or `eager_load`:

```ruby
# Good pattern
users = User.includes(:posts)
users.each { |user| puts user.posts.count }
```

- Use strong parameters in controllers for mass assignment protection:

```ruby
def user_params
  params.require(:user).permit(:name, :email, :password)
end
```

- Use scopes for common query patterns and concerns to share code between models.
- Use service objects for complex business logic that doesn't belong in models or controllers.

This rule provides guidance for Ruby on Rails development, helping you adhere to conventions and best practices while coding in the Rails framework. It's designed to assist with creating maintainable, efficient backend code that follows standard Rails patterns.

What this rule does

The Ruby on Rails rule acts as a specialized guide for backend development with the Rails framework. It encapsulates key conventions and best practices that help maintain code quality and consistency in Rails projects.

The rule focuses on several core aspects of Rails development:

Rails conventions

The rule emphasizes following Rails' established naming conventions, such as using singular names for models and plural names for controllers. It also recommends utilizing Rails generators for creating models, controllers, and migrations - leveraging Rails' built-in tooling for consistent code generation.

Database interaction best practices

To ensure efficient database interactions, the rule provides guidance on:

  • Using ActiveRecord methods instead of raw SQL when possible
  • Avoiding performance-killing N+1 queries through eager loading
  • Implementing proper security with strong parameters

For example, the rule recommends this pattern for avoiding N+1 queries:

# Good pattern
users = User.includes(:posts)
users.each { |user| puts user.posts.count }

Code organization strategies

The rule encourages sound code organization through:

  • Using scopes for common query patterns
  • Implementing concerns to share code between models
  • Creating service objects for complex business logic that doesn't belong in models or controllers

Using Ruby on Rails in Cursor

The Ruby on Rails rule is configured in the file ruby-on-rails.mdc within your .cursor/rules directory. Based on the glob pattern **/*.rb, this rule is automatically activated whenever you're working with Ruby files in your project.

This means when you open any .rb file in your Rails project, Cursor will automatically apply this context to the AI, making it aware of Rails conventions and best practices. The AI will then provide more Rails-aligned suggestions, code completions, and responses to your queries.

You can also manually invoke this rule by typing @ruby-on-rails in Cmd-K or chat if you need Rails-specific guidance while working in non-Ruby files (like JavaScript or HTML templates that interact with your Rails backend).

Usage tips

When to rely on this rule

This rule is most valuable when:

  • You're new to Rails development and need guidance on conventions
  • You're working on optimizing database queries
  • You're structuring controller actions and need reminders about strong parameters
  • You're deciding where to place complex business logic

Complementary practices

While using this rule, consider these complementary practices:

  • Run rails routes regularly to keep track of your application's routing structure
  • Use the Rails console (rails c) to test ActiveRecord queries before implementing them
  • Implement comprehensive tests for your service objects and complex model methods
  • Consider using Rubocop with Rails-specific extensions to further enforce style conventions

By leveraging this rule consistently, you'll develop Rails applications that are more maintainable, perform better, and align with community standards.

Want to 10x your AI skills?

Get a free account and learn to code + market your apps using AI (with or without vibes!).

Nah, maybe later