Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
khustochka opened this issue Mar 26, 2013 · 22 comments

Comments

@khustochka
Copy link

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.

@rafaelfranca
Copy link
Member

Is ActiveRecord loaded in your application?

@khustochka
Copy link
Author

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

@rafaelfranca
Copy link
Member

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

@khustochka
Copy link
Author

I will try to create a minimalistic app to reproduce.

@rafaelfranca
Copy link
Member

This will help a lot. thank you

@khustochka
Copy link
Author

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: rails/rails#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.

@rmoriz
Copy link

rmoriz commented Apr 2, 2013

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

@khustochka
Copy link
Author

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
Copy link
Author

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

kinopyo added a commit to kinopyo/rails-observers that referenced this issue Jun 27, 2013
Borrowed from melindaweathers's commit(couldn't find that one on her fork)
rails@e2fba81
@tvdeyen
Copy link

tvdeyen commented Jul 4, 2013

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

@tvdeyen
Copy link

tvdeyen commented Jul 4, 2013

Alternatively you can add

include ActionController::Caching::Sweeping

into your ApplicationController

@tiegz
Copy link
Contributor

tiegz commented Jul 12, 2013

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?

@guilleiguaran
Copy link
Member

Closed with #8

@Xuhao
Copy link

Xuhao commented Aug 5, 2013

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

@pmoran
Copy link

pmoran commented Aug 25, 2013

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

@tvdeyen
Copy link

tvdeyen commented Aug 25, 2013

Me too

@tiegz
Copy link
Contributor

tiegz commented Aug 25, 2013

@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.

@rafaelfranca rafaelfranca reopened this Sep 22, 2013
robinboening pushed a commit to robinboening/alchemy_cms that referenced this issue Nov 6, 2013
@peeyushsingla
Copy link

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

@WallyAli
Copy link

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

@amatsuda
Copy link
Member

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

@zaz
Copy link

zaz commented Aug 7, 2014

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?

dddaisuke pushed a commit to dddaisuke/quoty that referenced this issue Jul 12, 2015
Now at least can start the server and jump around between pages.

Problems with these gems:

* simple_form - update version to 3.0.0.rc
* friendly_id

friendly_id ~> 5.0.0 supports Rails 4, but it's not available in the
rubygems.org, so had to specify github repo and the alpha1 version.

gem 'friendly_id', '5.0.0.alpha1', github: 'FriendlyId/friendly_id'

The error of friendly_id may look like this:

> NoMethodError - undefined method `synchronize' for nil:NilClass:

* carrierwave

Had to make some changes manually for my PhotoUploader which
inheritents CarrierWave::Uploader::Base.

  class PhotoUploader < CarrierWave::Uploader::Base
    # include Sprockets::Helpers::RailsHelper
    # include Sprockets::Helpers::IsolatedHelper
    include Sprockets::Rails::Helper
  end

Was just copied from the commit:
carrierwaveuploader/carrierwave#661
carrierwaveuploader/carrierwave@c097370

* rails-observers

The error was like this:
Undefined method `cache_sweeper' or uninitialized constant ActionController::Caching::Sweeper

Was issued in rails/rails-observers#4

* protected_attributes
Ran into this error:
uninitialized constant ActiveRecord::MassAssignmentSecurity::NestedAttributes::ClassMethods::REJECT_ALL_BLANK_PROC

rails/protected_attributes#8

Had no time to try those pull requests, so just comment out for now.
@vinaysolanki
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests