rails-observers icon indicating copy to clipboard operation
rails-observers copied to clipboard

Undefined method `cache_sweeper' or uninitialized constant ActionController::Caching::Sweeper

Open khustochka opened this issue 12 years ago • 22 comments

I use cache sweepers in my project and try to make it work on Rails 4 master. I have added gem 'rails-observers', updated bundle. But I am still getting error undefined methodcache_sweeper' for ObservationsController:Class (NoMethodError)`.

I tried to debug and saw that rails-observers are actually loaded, but unfortunately I failed to go deeper into that eager_autoload stuff.

If I try to require my sweeper on top of controller I got error there: uninitialized constant ActionController::Caching::Sweeper. So it seems the values are not properly autoloaded

I tried both v 1.1.0 and edge version of the gem.

khustochka avatar Mar 26 '13 14:03 khustochka

Is ActiveRecord loaded in your application?

rafaelfranca avatar Mar 26 '13 14:03 rafaelfranca

Yes, sure. All other controllers, models work as expected

khustochka avatar Mar 26 '13 14:03 khustochka

The only reason that this is not working is that ActiveRecord constant is not defined

rafaelfranca avatar Mar 26 '13 14:03 rafaelfranca

I will try to create a minimalistic app to reproduce.

khustochka avatar Mar 26 '13 14:03 khustochka

This will help a lot. thank you

rafaelfranca avatar Mar 26 '13 14:03 rafaelfranca

https://github.com/khustochka/cache_sweeper_fail

If you run rake test you'll see a bunch of weird errors. This is because of this issue: https://github.com/rails/rails/issues/9933

The underlying problem is undefined method `cache_sweeper'. If you start the server and go to http://127.0.0.1:3000/posts you'll see this error.

khustochka avatar Mar 26 '13 15:03 khustochka

still having this issue, even with rails head

you can try my fork of @khustochka example (changed it from pg to sqlite)

cd /tmp
git clone https://github.com/rmoriz/cache_sweeper_fail.git
cd cache_sweeper_fail
bundle
RAILS_ENV=production bundle exec rake db:setup
#rake aborted!
#undefined method `cache_sweeper' for PostsController:Class
#/private/tmp/cache_sweeper_fail/app/controllers/posts_controller.rb:4:in `<class:PostsController>'
#/private/tmp/cache_sweeper_fail/app/controllers/posts_controller.rb:1:in `<top (required)>'
#/private/tmp/cache_sweeper_fail/config/environment.rb:5:in `<top (required)>'
#Tasks: TOP => db:setup => db:schema:load_if_ruby => environment
#(See full trace by running task with --trace)
#RAILS_ENV=production bundle exec rake db:setup  4,48s user 0,37s system 99% cpu 4,886 total

rmoriz avatar Apr 02 '13 19:04 rmoriz

soaring in the issue rails/rails#10294 was able to dig deeper

It reopens the module ActionController::Caching and include Sweeping, the module Sweeping extend > > ActiveSupport::Concern. In fact, this concern in sweeping doesn't works.

I modified include Sweeping if defined?(ActiveRecord) to ::ActionController::Caching.send(:include,Sweeping) if defined?(ActiveRecord)

It didn't work.

I added

at the beginning of the module Caching, It didn't work too.

I must explicitly use ActionController::Base to include Sweeping
``` ::ActionController::Base.send(:include, Sweeping) if defined?(ActiveRecord)

khustochka avatar Apr 30 '13 15:04 khustochka

As for my investigation there is also an Autoload issue. I debugged it in production env. When it reaches this line ActiveRecord::Observer is not defined. Observer is included in ActiveRecord using autoload and for now I fail to undestand how it works.

As a workaround I just manually require 'rails/observers/activerecord/observer' in my application.rb

khustochka avatar May 04 '13 06:05 khustochka

The hack from @melindaweathers works fine. Using the fork of @kinopyo for now.

tvdeyen avatar Jul 04 '13 20:07 tvdeyen

Alternatively you can add

include ActionController::Caching::Sweeping

into your ApplicationController

tvdeyen avatar Jul 04 '13 21:07 tvdeyen

I ran into Undefined method 'cache_sweeper' as well on Rails 4. Seems like Sweeping module is loaded too late. Here's the order:

  1. there are 2 modules attempted to be included in AC::Caching: RackDelegation and Callbacks. They aren't actually included yet, though, because AC::Caching is a Concern itself.

  2. the AC::Caching module is then included in AC::Base when AC::Base is loaded (and then RackDelegation and Callbacks are finally included as well, because they are dependencies of AC::Caching)

  3. later on, the AC::Caching::Sweeping module is attempted to be included in AC::Caching by rails-observers. It's too late though. AC::Caching::Sweeping is added as a 3rd dependency to AC::Caching and never included in AC::Base bc AC::Caching was included previously in #2

Any ideas? I feel like the answer lies in the railtie-- maybe removing Sweeping from Caching module and manually including Sweeping in the railtie?

tiegz avatar Jul 12 '13 02:07 tiegz

Closed with #8

guilleiguaran avatar Jul 18 '13 23:07 guilleiguaran

I still have this problem on version 0.1.2

#L51 always return false, so ActionController::Caching::Sweeper can't be defined.

I think it's because require order is not correct.

#L18 ran before #L8

Xuhao avatar Aug 05 '13 11:08 Xuhao

Like @Xuhao, I am also still getting problems in 0.1.2 (uninitialized constant ActionController::Caching::Sweeper). Fixed for now by blindly adding require "rails/observers/activerecord/active_record" to application.rb

pmoran avatar Aug 25 '13 12:08 pmoran

Me too

tvdeyen avatar Aug 25 '13 13:08 tvdeyen

@Xuhao @pmoran I haven't run into this yet, but I wonder if we can move the ActionController::Caching::Sweeper definition into the activerecord namespace in this gem? It seems more dependent on AR than AC.

tiegz avatar Aug 25 '13 16:08 tiegz

I am also facing this issue with rails 4 any solution?

peeyushsingla avatar Feb 27 '14 18:02 peeyushsingla

I'm getting "uninitialized constant ParentSweeper". any resolution yet?

WallyAli avatar May 25 '14 20:05 WallyAli

This error has to be fixed via #24 Try bundling GH master if you're still having this problem.

amatsuda avatar May 26 '14 03:05 amatsuda

Using the gem from the master branch, I'm also still getting this error. Example: jb/rails-sweeper-error

As a work around, I'm using an observer instead, and expiring the cache with ApplicationController.expire_page('/index.html'). But maybe I'm missing something?

zaz avatar Aug 07 '14 18:08 zaz

I fixed the error: uninitialized constant ActionController::Caching::Sweeper (NameError) by adding require "rails/observers/action_controller/caching" at the top of the sweeper file.

vinaysolanki avatar Aug 26 '21 14:08 vinaysolanki