diff --git a/activerecord/lib/active_record/connection_handling.rb b/activerecord/lib/active_record/connection_handling.rb index d472eb2e2b0c75ddc44d33fbdde14555a1e81067..e708772bf5a9f53239ab3abb02f8535e71fddda2 100644 --- a/activerecord/lib/active_record/connection_handling.rb +++ b/activerecord/lib/active_record/connection_handling.rb @@ -204,7 +204,7 @@ def connection # Return the specification name from the current class or its parent. def connection_specification_name if !defined?(@connection_specification_name) || @connection_specification_name.nil? - return primary_class? ? "primary" : superclass.connection_specification_name + return self == Base ? "primary" : superclass.connection_specification_name end @connection_specification_name end diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index 843242a89746fc584e8289efb06971e70c081303..f2fa6f12cf208337a3ec0a3cbf4bff00f7085cef 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -375,6 +375,8 @@ class MyClass < ApplicationRecord end def test_connection_specification_name_should_fallback_to_parent + Object.send :const_set, :ApplicationRecord, ApplicationRecord + klassA = Class.new(Base) klassB = Class.new(klassA) klassC = Class.new(MyClass) @@ -387,6 +389,12 @@ def test_connection_specification_name_should_fallback_to_parent klassA.connection_specification_name = "readonly" assert_equal "readonly", klassB.connection_specification_name + + ActiveRecord::Base.connection_specification_name = "readonly" + assert_equal "readonly", klassC.connection_specification_name + ensure + Object.send :remove_const, :ApplicationRecord + ActiveRecord::Base.connection_specification_name = "primary" end def test_remove_connection_should_not_remove_parent