rails-performancelisted
Install: claude install-skill mickzijdel/rails-toolkit
# Rails Performance Patterns
## 1. Fragment Caching in Views
Cache expensive partials with `<% cache record do %>`. The key derives from `cache_key_with_version`, so the fragment invalidates when the record's `updated_at` changes. Nest cache blocks for granular invalidation.
```erb
<% cache card do %>
<div class="card-perma__actions">
<%= render "cards/container/gild", card: card %>
<%= render "cards/container/image", card: card %>
</div>
<%= card_article_tag card, class: "card" do %>
<%= render "cards/display/perma/board", card: card %>
<%= render "cards/display/perma/tags", card: card %>
<% end %>
<% end %>
```
---
## 2. Collection Caching
`cached: true` on collection renders caches each item individually and fetches all of them in one `read_multi` call. Combine with a `preloaded` scope (Pattern 7) to avoid N+1 during rendering.
```erb
<%= render partial: "cards/comments/comment",
collection: card.comments.preloaded.chronologically,
cached: true %>
```
---
## 3. JSON Caching
Use `json.cache!` in Jbuilder templates:
```ruby
json.cache! @event do
json.id @event.id
json.action @event.action
json.eventable do
json.partial! @event.eventable
end
end
```
---
## 4. Don't Over-Cache: The "Fast N+1" Anti-Pattern
Granular `Rails.cache.fetch` calls scattered through models and helpers turn a list page into dozens or hundreds of cache round-trips — an N+1 made of cache reads instead of queries. Each read is a networ