Performance: Scopes for reusable queries
MEDIUM IMPACT
This concept affects database query efficiency and page load speed by reusing query logic and reducing redundant database calls.
class User < ApplicationRecord scope :active, -> { where(active: true) } scope :recent, -> { where('created_at > ?', 1.month.ago) } scope :admins, -> { where(role: 'admin') } end User.active.recent.admins
User.where(active: true).where('created_at > ?', 1.month.ago).where(role: 'admin')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated inline queries | N/A | N/A | N/A | [X] Bad |
| Reusable scopes with chaining | N/A | N/A | N/A | [OK] Good |