Forum: Ruby on Rails Sweeper class doesn't work

Posted by Trung Do (trungdo)
on 2010-05-13 12:07
Hi experts,

I have built a typical cache sweeper class:

#======================
class PrivilegeCacheSweeper < ActionController::Caching::Sweeper
  observe User

  def after_update(user)
   expire_cache_for(user)
  end


  private

  def expire_cache_for(user)
    p "Hi there!!!"
    expire_action(:controller => "users", :action => "user_view")
  end
end
#======================

In the UsersController I set:
#======================
class UsersController < ApplicationController

  caches_action :user_view
  cache_sweeper :privilege_cache_sweeper

  def user_view
    #code to show list of accessible units of login user
  end

  def update
    #update user
  end
end
#======================

In environment.rb, I already set:
#======================
config.action_controller.perform_caching = true
#======================

The problem is that after the user is updated, the page "user_view" is
still cached when being refreshed (The output shows "Cached fragment
hit: views/localhost:3000/users/user_view").

The output also shows the text "Hi there!!!" => It means method
"expire_cache_for(user)" is called. However, "expire_action()" seems not
to work at all.

I also tested the expire_action(:controller => "users", :action =>
"user_view") by inserting it directly in UsersController::update method.
It works fine!

Could you give me any suggestion to solve this problem?

My application runs on:

Rails: 2.3.2
Ruby: 1.8
OS: Ubuntu 8.04

Thank you very much.
Posted by Trung Do (trungdo)
on 2010-05-15 03:18
Hi,

For whom may get the same problem:

I moved the sweeper declaration 'cache_sweeper :privilege_cache_sweeper' 
from UsersController to ApplicationController. And it works for me!

But I still have no idea why this change makes expire_action in cache 
sweeper work.

Any explanations are welcome.

Thanks and regards,

.Viet Trung.
Posted by Josh Ellithorpe (quest)
on 2010-07-18 09:06
Viet,

I signed up today just to say you are my savior. I couldn't figure out 
why the hell my sweeper wasn't working either.

Thanks for the working fix.
Posted by Simon K. (simon_k29)
on 2012-12-10 00:33
Over two years later .. same problem, same fix, thanks!!
But I would also like an answer to the original question .. because i 
also don't understand why it only works in the AppController
Posted by tamouse mailing lists (Guest)
on 2012-12-10 03:46
(Received via mailing list)
On Sun, Dec 9, 2012 at 5:33 PM, Simon K. <lists@ruby-forum.com> wrote:
> Over two years later .. same problem, same fix, thanks!!
> But I would also like an answer to the original question .. because i
> also don't understand why it only works in the AppController

-ENOCONTEXT
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.