Caching, Collections, Rails 5, multiple views colliding

I have a Thing object that can be presented in two ways depending on whether you are in the main controller or the list controller.



I can cache both of these without them clobbering each others’ keys


<% cache [“main”,thing] do %>


<% end %>



<% cache [“list”,thing] do %>


<% end %>

Rails 5 has the shiny new cache capability for collections

<%= render partial: “main/thing”, collection: myThings, cached: true %>

this works great

however I’d also like to the same approach in my list view with

<%= render partial: “list/thing”, collection: myThings, cached: true %>

but now I have conflicting keys and everything goes squirly. I think because the cache key is generated on Thing and doesn’t ‘realise’ that I’m using different presentation logic.

there is one hint of an error I get

‘Couldn’t find template for digesting: list/_thing’

I don’t understand this error; list/_thing.html.erb definitely does exist.

I can’t for the life of me figure out how to get a custom cache key (and for a while I accidentally broke production before realising the issue here).

The relevant rails code is here I think

and it looks like this should be possible.

I have tried

cached: -> myThings { [ myThings, “list” ] }


cached: [myThings, “list” ]

but those were really just guesses as I don’t understand the logic

I’m running rails 5.0.6

can anyone help?

thank you.