Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

uninitialized constant ActionController::Caching::Sweeper #3729

Closed
jesseclark opened this Issue · 6 comments

7 participants

@jesseclark

I'm using rails 3.1.3 and ruby 1.9.2.

Following the Rails Caching Guide I have created a sweeper to clear memcached when related objects get modified.

class MedicalProfileSweeper < ActionController::Caching::Sweeper
  observe MedicalProfile, User, Study

  def expire_cached_content(mp)
    Rails.cache.delete("matches_for_#{mp.user.id}")
  end

  alias_method :after_save, :expire_cached_content
  alias_method :after_destroy, :expire_cached_content

end

When I try to start the server the app pukes with this error:
/app/sweepers/medical_profile_sweeper.rb:1:in `': uninitialized constant ActionController::Caching::Sweeper (NameError)

I have added app/sweepers to the autoload_paths.
I am not using ActiveRecord in this app and this is how I am requiring the rails libs that I am using:

require "active_resource/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "rails/test_unit/railtie"
require "sprockets/railtie"
@jesseclark

Ah so, coming back to look at this issue fresh, I went and looked in actionpack/lib/action_controller/sweeping and noticed that in two places this Module looks checks for the presence of ActiveRecord, most notably the entire Sweeper class definition is wrapped in this check:

    if defined?(ActiveRecord) and defined?(ActiveRecord::Observer)
      class Sweeper < ActiveRecord::Observer #:nodoc:

which I guess makes sense because Sweepers inherit from Observers which is in AR.

It would be really helpful if the ActiveRecord dependency for using Sweepers was mentioned in the Rails Caching Guide and the module documentation.

Luckily, Mongoid has an Observer class so I can still get this functionality implemented.

If I get time soon I will submit a documentation patch.

@masukomi

encountering the same issue. Also using Mongoid. Just noting so that folks don't think it's only Jesse who's having do work around it.

@pgib

Having the same issue with Mongoid.

@jeremyf

@jesseclark Did you have a chance to submit the documentation? Also are you not loading ActiveRecord in your config?

(@jeremyf)

@schneems
Collaborator

I added documentation, can you please close this issue?

rails/docrails@c9d1ba4

@sgerrand sgerrand referenced this issue from a commit in sgerrand/rails
@schneems schneems Doc: sweepers only work on Active Record Models
In response to this rails issue: rails#3729
c9d1ba4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.