Wrong partial is rendered in Rails in some cases, possible fragment caching issue
Asked Answered
A

0

8

I have a template that displays a list of events

<tbody>
<%= render partial: 'event', collection: events, cached: true %>
</tbody>

The partial event:

<% cache event do %>
  <tr>
    <td>
      Something
    </td>
    <td>
      <%= render 'identifiable_link_with_tag', identifiable: event.identifiable %>
    </td>
  </tr>
<% end %>

The partial identifiable_link_with_tag:

<% cache identifiable do %>
    <span class="badge badge-info"><%= identifiable.type %></span> <%= link_to identifiable.identifier, identifiable %>
<% end %>

Now, the odd thing is what follows. Sometimes I notice in the events view that for some events another partial (identifiable) is rendered instead of identifiable_link_with_tag: _identifiable. This seems very odd, and on a page that lists 25 events, this would only happen for 1 or 2 or 3 (or 0) events.

So in short, it seems that sometimes the wrong identifiable is rendered. I do use Rails fragment caching, so that may be a factor. Am I missing something or have I encountered a Rails bug? This issue is very hard to reproduce in development, thus hard to debug.

Amandaamandi answered 3/10, 2017 at 20:52 Comment(7)
Are you getting the wrong event partial or identifiable_link_with_tag? What exactly is wrong? One thing with Russian doll caching is that update to any identifiable will not expire the event partial it is nested in unless that event is touched. Not sure if that's the problem you're facing.Semidiurnal
@EJ2015 it's not an issue regarding outdated information, I am aware of that. Instead of the partial identifiable_link_with_tag, the partial for identifiable itself is rendered (see updated answer)Amandaamandi
Are you also caching the partial identifiable? Maybe that is using the same cache key as the corresponding identifiable_link_with_tag, if you're also doing cache identifiable there too? If so you can try using a custom cache key for one of them.Semidiurnal
@EJ2015 Yes I am also caching identifiable partial! I was thinking of some kind of cache key collision too, but if so is this a bug in Rails or not?Amandaamandi
The key should include a template digest. So this seems unlikely. But this is the only thing I can think of. Can you test it?Semidiurnal
Please check server logs carefully and try to update the snippet when it renders different partial than the one you mentioned in views. @AmandaamandiElephant
Consider adding the content of the identifiable_link_with_tag and identifiable partials. This will probably help with debugging.Mcshane

© 2022 - 2024 — McMap. All rights reserved.