diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index d88be4effcb..5fe3c1bd633 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -394,7 +394,11 @@ To keep using the current cache store, you can turn off cache versioning entirel config.after_initialize do unless app.config.cache_classes Rails.autoloaders.main.on_unload do |_cpath, value, _abspath| - value.current_scope = nil if value.is_a?(Class) && value < ActiveRecord::Base + # Conditions are written this way to be robust against custom + # implementations of value#is_a? or value#<. + if Class === value && ActiveRecord::Base > value + value.current_scope = nil + end end end end diff --git a/railties/test/application/initializers/frameworks_test.rb b/railties/test/application/initializers/frameworks_test.rb index 8a29c6c52a3..efdef657d3f 100644 --- a/railties/test/application/initializers/frameworks_test.rb +++ b/railties/test/application/initializers/frameworks_test.rb @@ -342,8 +342,25 @@ module ApplicationTests test "Current scopes in AR models are reset on reloading" do rails %w(generate model post) rails %w(db:migrate) + + app_file "app/models/a.rb", "A = 1" + app_file "app/models/m.rb", "module M; end" + app_file "app/models/post.rb", <<~RUBY + class Post < ActiveRecord::Base + def self.is_a?(_) + false + end + + def self.<(_) + false + end + end + RUBY + require "#{app_path}/config/environment" + A + M Post.current_scope = Post assert_not_nil ActiveRecord::Scoping::ScopeRegistry.current_scope(Post) # precondition