feature: Using locals to generate custom cache key when rendering collection

Not sure if this is a quality idea, or not. When rendering a collection, could a custom cache key be generated using the object and any locals being passed? Example: including current_user in the collection cache key

Hey Adam,

That’s already possible. See the Collection Caching section here: https://api.rubyonrails.org/classes/ActionView/Helpers/CacheHelper.html#method-i-cache

<%= render partial: 'projects/project', collection: @projects, cached: -> project { [ project, current_user ] } %>
1 Like

How did I miss that??!! I must have read through those docs a million times. Thank you so much for posting.

Glad to hear it!

I just realized we should probably move that section to the render method instead. Since that’s where the option is.

So there’s a free PR there for anyone who’s interested!

I would love to contribute. So when you said move Collection caching section to render, did you mean to move out the method `cache` from `cache_helper` to `render_helper`? Or do we need to just move the documentation to render method about collection caching?

Collection caching is also mentioned here Caching with Rails: An Overview — Ruby on Rails Guides . Do we need to add `cached: block` here?

Just the documentation section. #cache in render_helper doesn’t make sense.

A guide shouldn’t be an exhaustive list of every possible option, so let’s skip that too.