Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Rails, a Sweeper isn't getting called in a Model-only setup
    text
    copied!<p>I'm working on a Rails app, where I'm using page caching to store static html output. The caching works fine. I'm having trouble expiring the caches, though.</p> <p>I believe my problem is, in part, because I'm not expiring the cache from my controller. All of the actions necessary for this are being handled within the model. This seems like it should be doable, but all of the references to Model-based cache expiration that I'm finding seem to be out of date, or are otherwise not working.</p> <p>In my environment.rb file, I'm calling</p> <pre><code>config.load_paths += %W( #{RAILS_ROOT}/app/sweepers ) </code></pre> <p>And I have, in the /sweepers folder, a LinkSweeper file:</p> <pre><code>class LinkSweeper &lt; ActionController::Caching::Sweeper observe Link def after_update(link) clear_links_cache(link) end def clear_links_cache(link) # expire_page :controller =&gt; 'links', :action =&gt; 'show', :md5 =&gt; link.md5 expire_page '/l/'+ link.md5 + '.html' end end </code></pre> <p>So ... <b>why isn't it deleting the cached page when I update the model?</b> (Process: using script/console, I'm selecting items from the database and saving them, but their corresponding pages aren't deleting from the cache), and I'm also calling the specific method in the Link model that would normally invoke the sweeper. Neither works.</p> <p>If it matters, the cached file is an md5 hash off a key value in the Links table. The cached page is getting stored as something like /l/45ed4aade64d427...99919cba2bd90f.html.</p> <p>Essentially, it seems as though the Sweeper isn't actually observing the Link. I also read (<a href="http://codelevy.com/2008/03/04/rails-caching-sweepers-controllers-and-models" rel="noreferrer">here</a>) that it might be possible to simply add the sweeper to config.active_record.observers in environment.rb, but that didn't seem to do it (and I wasn't sure if the load_path of app/sweepers in environment.rb obviated that).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload