Skip to content
This repository
Browse code

Reset default scope in Thread.current when class is unloaded [#5497 s…

…tate:resolved]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
commit 4e67bf26aa11a59e4f513359208fe3314c5a1ba5 1 parent f196633
Andrew White authored fxn committed
8  activerecord/lib/active_record/base.rb
@@ -890,6 +890,10 @@ def scoped_methods #:nodoc:
890 890
         Thread.current[key] = Thread.current[key].presence || self.default_scoping.dup
891 891
       end
892 892
 
  893
+      def before_remove_const #:nodoc:
  894
+        reset_scoped_methods
  895
+      end
  896
+
893 897
       private
894 898
 
895 899
         def relation #:nodoc:
@@ -1174,6 +1178,10 @@ def current_scoped_methods #:nodoc:
1174 1178
           scoped_methods.last
1175 1179
         end
1176 1180
 
  1181
+        def reset_scoped_methods #:nodoc:
  1182
+          Thread.current[:"#{self}_scoped_methods"] = nil
  1183
+        end
  1184
+
1177 1185
         # Returns the class type of the record using the current module as a prefix. So descendants of
1178 1186
         # MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
1179 1187
         def compute_type(type_name)
15  activerecord/test/cases/base_test.rb
@@ -1415,6 +1415,21 @@ def test_compute_type_no_method_error
1415 1415
     end
1416 1416
   end
1417 1417
 
  1418
+  def test_default_scope_is_reset
  1419
+    Object.const_set :UnloadablePost, Class.new(ActiveRecord::Base)
  1420
+    UnloadablePost.table_name = 'posts'
  1421
+    UnloadablePost.class_eval do
  1422
+      default_scope order('posts.comments_count ASC')
  1423
+    end
  1424
+
  1425
+    UnloadablePost.unloadable
  1426
+    assert_not_nil Thread.current[:UnloadablePost_scoped_methods]
  1427
+    ActiveSupport::Dependencies.remove_unloadable_constants!
  1428
+    assert_nil Thread.current[:UnloadablePost_scoped_methods]
  1429
+  ensure
  1430
+    Object.class_eval{ remove_const :UnloadablePost } if defined?(UnloadablePost)
  1431
+  end
  1432
+
1418 1433
   protected
1419 1434
     def with_env_tz(new_tz = 'US/Eastern')
1420 1435
       old_tz, ENV['TZ'] = ENV['TZ'], new_tz

0 notes on commit 4e67bf2

Please sign in to comment.
Something went wrong with that request. Please try again.