rails-antipattern-spaghetti-sql
Installation
SKILL.md
Antipattern: Spaghetti SQL
The smell
- The same business concept ("published, recent posts") expressed slightly differently across many call sites
where("...")strings in controllers and viewsfind_by_sqland rawconnection.executeoutside of migrations
Why it hurts
- A schema or business-rule change requires hunting every call site
- Easy to drift (one place forgets
featured, another forgets a tenancy filter) - SQL fragments bypass scopes, joins, and security/tenancy filters
- Test setup mirrors the SQL instead of the intent
The fix
- Push every query into a named scope or class method on the model
- Name scopes for the business term, not SQL:
published,featured,recent,reverse_chronologically - For complex queries that don't compose, use a query object PORO (
PostsForFeed) - Call sites read like sentences:
Post.published.featured.recent.limit(10)