rails
✓Verified·Scanned 2/18/2026
Build Rails applications with proper conventions, performance patterns, and security practices.
from clawhub.ai·vc256c0e·4.4 KB·0 installs
Scanned from 1.0.0 at c256c0e · Transparency log ↗
$ vett add clawhub.ai/ivangdavila/rails
Rails Development Rules
Active Record
- N+1 queries are the #1 performance killer — use
includes,preload, oreager_load find_eachfor large datasets —all.eachloads everything in memorywhere.not(field: nil)instead ofwhere("field IS NOT NULL")— more portableupdate_allanddelete_allskip callbacks — use intentionally, not accidentallypluck(:id)when you only need values — avoids instantiating full models
Associations
dependent: :destroycascades deletion — forgetting it orphans recordshas_many :throughfor many-to-many with join model —has_and_belongs_to_manyhas no modelinverse_ofhelps Rails reuse loaded objects — especially important with nested formstouch: trueupdates parent timestamp — useful for cache invalidation
Migrations
- Never edit committed migrations — create new migration to fix, rollback breaks teammates
add_indexin separate migration for large tables — can lock table for minutesnull: falseanddefault:in migration — don't rely on model validations alonechangemethod must be reversible — useup/downfor complex changes- Foreign keys with
add_foreign_key— database-level integrity beyond model validations
Controllers
- Strong parameters:
params.require(:model).permit(:fields)— whitelist explicitly before_actionfor shared logic — but don't nest too deep, hard to tracerespond_toblock for format handling — JSON APIs and HTML from same action- Avoid business logic in controllers — extract to models or service objects
redirect_toends request — but code after it still runs, usereturnorand return
Views and Partials
render collection:is faster than loop withrender partial:— single partial render vs manycachehelper with model —cache @post doauto-expires on updatecontent_forandyieldfor layout sections — not instance variablesturbo_frameandturbo_streamfor Hotwire — replace full page reloads
Security
protect_from_forgeryon by default — don't disable CSRF without understanding- SQL injection: never interpolate user input in queries — always use
?placeholders or hashes - Mass assignment: strong parameters prevent attribute injection — controller level, not model
html_safeandrawbypass escaping — only for trusted contentsecureandhttponlycookie flags — enabled by default in production
Background Jobs
- Sidekiq or Solid Queue for async processing — don't use
delayin request cycle - Jobs should be idempotent — they may run multiple times on retry
- Pass IDs not objects — serialized objects break if class changes
perform_laterqueues,perform_nowblocks — use later except in tests
Caching
- Russian doll caching: nest
cacheblocks — inner changes bust only inner cache - Fragment caching with
cache_key_with_version— automatic invalidation Rails.cache.fetchwith block — cache computation result- Low-level caching needs explicit expiration — fragments auto-expire with model changes
Testing
FactoryBotover fixtures for most cases — more flexible, less brittleletis lazy,let!is eager — uselet!when you need side effectsfreeze_timefor time-dependent tests —travel_toblock reverts automaticallyassert_differencefor counting changes — clearer intent than before/after counts- System tests with Capybara — slower but test full stack including JS
Performance
bulletgem catches N+1 in development — essential for any Rails apprack-mini-profilershows query time — visible in developmentActiveRecord::Base.logger = nilin console for quiet queries- Database indexes on foreign keys and frequent
wherecolumns — checkexplainoutput
Common Mistakes
savevssave!— first returns boolean, second raises on failureupdatereturns false on failure — check return value or useupdate!||=memoization cachesnilandfalse— usedefined?pattern for those- Callbacks creating complex chains — consider service objects instead
default_scopeaffects all queries including joins — almost always a mistake