---
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.
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:
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.
To ensure efficient database interactions, the rule provides guidance on:
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 }
The rule encourages sound code organization through:
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).
This rule is most valuable when:
While using this rule, consider these complementary practices:
rails routes
regularly to keep track of your application's routing structurerails c
) to test ActiveRecord queries before implementing themBy leveraging this rule consistently, you'll develop Rails applications that are more maintainable, perform better, and align with community standards.