Medium Rails

Use scopes for common queries

Rule Description

Define scopes for frequently used query conditions:
- Makes code more readable
- Enables method chaining
- Centralizes query logic
- Easier to test

```ruby
class Order < ApplicationRecord
scope :recent, -> { where('created_at > ?', 1.week.ago) }
scope :completed, -> { where(status: 'completed') }
scope :for_user, ->(user) { where(user: user) }
end

# Usage
Order.recent.completed.for_user(current_user)
```

Included in Rulesets

Rails Standards
36 rules • 1 standard
View
Code Review Standards
58 rules • 0 standards
View

Add This Rule

Sign in to add this rule to your workspace

Sign in with GitHub

Details

Severity
Medium
Category
Rails
Used in
2 rulesets