In my project I use database multi-tenancy 'apartment' gem.
config/initializers/apartment.rb
Apartment.configure do |config|
config.excluded_models = %w{ User Company }
end
To clean up the database it tests I use 'database_cleaner' gem
spec/rails_helper.rb
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do |example|
DatabaseCleaner.strategy= example.metadata[:js] ? :truncation : :transaction
DatabaseCleaner.start
Apartment::Tenant.switch!('app')
end
config.after(:each) do
Apartment::Tenant.switch!
DatabaseCleaner.clean
end
end
In RSpec tests with Capybara truncation strategy clean after each test only public schema where only the user and company.
Before test start
Company.count#=> 0
Other schemes are not cleared.
Before test start
SomeModelInCompanySchema.count#=> 240
How to clear data in another scheme