views:

299

answers:

1

I'm trying to setup some caching on my site and am having troubles with getting a cache sweeper working. I followed the Railscast but when I try to load a page with the model, I'm getting an error. Here's what I've done thus far:

1.) Added the app/sweepers directory and put a basic sweeper in it:

class TeamMemberSweeper < ActionController::Caching::Sweeper
  observe TeamMember

  def after_create(team_member)
    expire_nav_menus
  end

  def after_destroy(team_member)
    expire_nav_menus
  end

  private

  def expire_nav_menus(athlete_id)
    expire_fragment(...)
  end
end

2.) Add the following line to the TeamMember model:

cache_sweeper :team_member_sweeper, :only => [:create, :destroy]

3.) Added the following line to config/environment.rb and restarted my server:

config.load_paths += %W( #{RAILS_ROOT}/app/sweepers )

At this point, whenever I access the TeamMember model I get the following error:

undefined method `cache_sweeper' for #<Class:0x23128cc>

What I missing to get this working?

+3  A: 

I think because the

"cache_sweeper :team_member_sweeper, :only => [:create, :destroy]"

Should go in the controllers you want to expire in and not the model.

I've ran into similar problems because I specifically WANT to expire from the model, but apparently that's not possible or very hard to do in Rails.

Marston A.
Yup--you add cache sweepers in the controller, not the model.
zenazn