seancdavis
seancdavis

Reputation: 2821

Rails 4 cache_digests Not Catching Changes

I'm attempting to use fragment caching with Rails 4 to cache my navigation menu.

Model

class Page < ActiveRecord::Base

  belongs_to :parent, :class_name => 'Page', :touch => true

  has_many :children, :class_name => 'Page', 
    :foreign_key => 'parent_id'

  ...

end

This part is working fine. Whenever I update a nested page, all parents' updated_at values are changed. But a new cache view file for that group of pages is not written.

Navigation View

<nav>
  <ul>
    <% Page.level_1.each do |page_1| %>
      <% cache page_1 do %>
      <li>
        <%= link_to page_1.title, level_1_page_path(page_1.slug) %>
        <% if page_1.children.count > 0 %>
          # continue nesting children
          ...
        <% end %>
      </li>
      <% end %>
    <% end %>
  </ul>
</nav>

Why isn't Rails recognizing the change in the page?

Upvotes: 1

Views: 76

Answers (1)

seancdavis
seancdavis

Reputation: 2821

It turns out I had everything correct, but my model's scope:

scope :level_1, where(:level => 1)

was being cached by default, as it didn't contain lambda. Changing the scope to:

scope :level_1, -> { where(:level => 1) }

solved my problem and now cache digests work perfectly.

Upvotes: 1

Related Questions