This is for Ruby 1.8.7 & Rails 3.0.1.
I have a model TeachingGroup
, which belongs to current_user.current_term
.
A Sweeper exists with:
class TeachingGroupSweeper < ActionController::Caching::Sweeper
observe TeachingGroup
def after_update(teaching_group)
expire_cache_for(teaching_group) # elsewhere, working
end
... # other actions, expire_cache_for method
end
Expiring the cache works fine for the create action
, but not for destroy
or update
. The problem is this: In the controller, I fetch the TeachingGroup
using the @current_term variable, to prevent users updating models that don't belong to them, like so:
def update
@teaching_group = @current_term.teaching_groups.find(params[:id])
if @teaching_group.update_attributes( params[:teaching_group] )
flash[:notice] = "#{TeachingGroup.model_name.human} #{@teaching_group} aktualisiert."
end
respond_with @teaching_group, :location => edit_school_term_path(@current_term)
end
Now, this will not trigger the sweeper. If I change the line to:
@teaching_group = TeachingGroup.find(params[:id])
Everything works fine, but I lose the security. I tried adding
observe TeachingGroup, current_user.current_term.teaching_groups
to the sweeper, but not surprisingly this doesn’t work. Am I doing something wrong, or is this a bug? Any help would be appreciated. Thanks!