Medium
Rails
Use scopes for common queries
Official
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)
```
Add This Rule
Sign in to add this rule to your workspace
Sign in with GitHubDetails
- Severity
- Medium
- Category
- Rails
- Used in
- 2 rulesets