V
V
vadimstroganov2015-12-13 15:29:39
Ruby on Rails
vadimstroganov, 2015-12-13 15:29:39

Is caching done correctly?

Hello!
There is a partial menu.
I want to cache this fragment, it seems to work, but when updating the entry in the menu, the partial remains the same, the cache is not reset, I don’t understand what’s wrong.
_menu.slim

- menu = render_menu(code)

ul class="nav navbar-nav navbar-right"
  = cache ['menu', Menu.latest]
    - menu.each do |item|
      = cache item do
        li
          = (item.page.template.name == "index") ? link_to(item.page.title, "/") : link_to(item.page.title, item.page.slug)

In the Menu.rb model
def self.latest
  MenuTranslation.order(:updated_at).last
end

application_helper.rb:
def render_menu(code)
  menu = Menu.find_by :code => code
  menu.children
end

There are no created_at, updated_at fields in the menus table, they are in menu_translations (the translations of the menu item are stored there)
And another question, the link to the menu item itself is stored in the menus table, that is, I need to add updated_at there, and then monitor updated_at in two tables?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zaporozhchenko Oleg, 2015-12-13
@c3gdlk

The cache function can take either a ready-made key for storage, or, as in your case, a model instance. In this case, the key will be built from id and updated_at, so updated_at must be in all models.
It is also called before the first cache in your code
, in this case there is no point in the first cache call. The
easiest way is to look at the console logs;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question