diff --git a/lib/database_cleaner/active_record/base.rb b/lib/database_cleaner/active_record/base.rb index ed61045f..51a3e341 100644 --- a/lib/database_cleaner/active_record/base.rb +++ b/lib/database_cleaner/active_record/base.rb @@ -84,8 +84,11 @@ def lookup_from_connection_pool end def establish_connection - ::ActiveRecord::Base.establish_connection(connection_hash) - ::ActiveRecord::Base + # creates a temporary ActiveRecord class that is unique to connection_hash + ActiveRecord.module_eval("class Temp#{connection_hash.hash.abs} < ::ActiveRecord::Base; self; end") + .tap { |ar_class| + ar_class.establish_connection(connection_hash) + } end def database_for(model) diff --git a/spec/database_cleaner/active_record/base_spec.rb b/spec/database_cleaner/active_record/base_spec.rb index 652ca9cd..73efdee8 100644 --- a/spec/database_cleaner/active_record/base_spec.rb +++ b/spec/database_cleaner/active_record/base_spec.rb @@ -166,7 +166,8 @@ module ActiveRecord it "establishes a connection with it" do expect(::ActiveRecord::Base).to receive(:establish_connection).with(hash) - expect(strategy.connection_class).to eq ::ActiveRecord::Base + expect(strategy.connection_class).to be < ::ActiveRecord::Base + expect(strategy.connection_class.name).to match /^DatabaseCleaner::ActiveRecord::Temp\d+$/ end end