Undefined method `cache_sweeper' or uninitialized constant ActionController::Caching::Sweeper
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.
Is ActiveRecord loaded in your application?
Yes, sure. All other controllers, models work as expected
The only reason that this is not working is that ActiveRecord constant is not defined
I will try to create a minimalistic app to reproduce.
This will help a lot. thank you
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.
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
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)
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
The hack from @melindaweathers works fine. Using the fork of @kinopyo for now.
Alternatively you can add
include ActionController::Caching::Sweeping
into your ApplicationController
I ran into Undefined method 'cache_sweeper' as well on Rails 4. Seems like Sweeping module is loaded too late. Here's the order:
-
there are 2 modules attempted to be included in
AC::Caching:RackDelegationandCallbacks. They aren't actually included yet, though, becauseAC::Cachingis aConcernitself. -
the
AC::Cachingmodule is then included inAC::BasewhenAC::Baseis loaded (and thenRackDelegationandCallbacksare finally included as well, because they are dependencies ofAC::Caching) -
later on, the
AC::Caching::Sweepingmodule is attempted to be included inAC::Cachingbyrails-observers. It's too late though.AC::Caching::Sweepingis added as a 3rd dependency toAC::Cachingand never included inAC::BasebcAC::Cachingwas 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?
Closed with #8
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.
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
Me too
@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.
I am also facing this issue with rails 4 any solution?
I'm getting "uninitialized constant ParentSweeper". any resolution yet?
This error has to be fixed via #24 Try bundling GH master if you're still having this problem.
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?
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.