2016-03-31 4 views
1

Dans mon application rails, j'essaie d'utiliser des caches imbriqués, mais ma clé de cache n'expire pas lorsque user.profile.full_name est modifiée. Ainsi, lorsque l'utilisateur change de nom, le nom complet affiché par _profile_product.html.erb reste l'ancien.rails 4 expiration du cache ne fonctionne pas

Comment changer la clé?

profils/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 %> 

Répondre

1

Essayez de changer la clé de cache dans

_profile_p roducts.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 %> 

Le problème est que la cachefragment qui contient toute la liste ne prend pas fin lorsqu'un utilisateur mis à jour son nom de profil. En ajoutant le maximum du updated_at du profil d'utilisateur associé à la clé de cache, le fragment de cache expirera lorsqu'un utilisateur met à jour son profil.