Срок действия кеша rails 4 не работает

В моем приложении rails я пытаюсь использовать вложенные кеши, но срок действия моего ключа кеша не истекает при изменении user.profile.full_name. Поэтому, когда пользователь меняет свое имя, полное_имя, отображаемое _profile_product.html.erb, остается старым.

Как мне сменить ключ?

профили/show.html.erb

<% cache(@profile) do %> #this is the profile info and the cache key expires properly when @profile.full_name changes
  <%= @profile.full_name %>
  .....
<% end %>
<% if @profile.user.products.any? %> #not nested in the previous cache; 
  #products belonging to the profile are listed with this code under the profile info
  <%= render 'products/profile_products' %>
<% end %>

_profile_products.html.erb

<% cache(['profile-products', @profile_products.map(&:id), @profile_products.map(&:updated_at).max]) do %>
  <%= render partial: "products/profile_product", collection: @profile_products, as: :product %>
<% end %>

_profile_product.html.erb

<% cache (['profile-product-single', product, product.user.profile]) do %>
  <%= product.name %>
  <%= product.user.profile.full_name %> #if I change profile name this one won't change thanks to the cache
<% end %>

person Sean Magyar    schedule 31.03.2016    source источник


Ответы (1)


Попробуйте изменить ключ кеша в

_profile_products.html.erb

<% cache(['profile-products', @profile_products.map(&:id), @profile_products.map(&:updated_at).max, @profile_products.map{|pp| pp.user.profile.updated_at.to_i }.max]) do %>
  <%= render partial: "products/profile_product", collection: @profile_products, as: :product %>
<% end %>

Проблема в том, что срок действия кэш-фрагмента, содержащего весь список, не истекает, когда пользователь обновляет имя своего профиля.

При добавлении максимального значения updated_at связанного профиля пользователя к ключу кеша срок действия фрагмента кеша истечет, когда пользователь обновит свой профиль.

person Jacob Rastad    schedule 31.03.2016