Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
Views
Good approach to switch database for Test mode / Sand box mode : Rails 6

I am working on an app where I now need to add test mode or sandbox mode features like Stripe provides in the dashboard. In this feature, when the user turned the test mode on, the user can test the functionality and can create dummy data in the same login session/token.

I have tried to use the Rails 6 feature to use multiple databases but have a few questions:

  • is it good to switch the connection in production for test mode even we will not have many test requests?
  • will it be good to have a separate instance for test mode with the test subdomain? in this case how we should manage the login sessions? should we copy data to the test the database? will it be good and common practice?

I only can manage to implement this if I have user data in the test database so that when I switch database connection system will not send an unauthenticated response.

Note:

  • for login and user update, it will use the primary database all time and for other actions, it will use the test_mode database. I am doing this by skip_around_action in a specific controller.
  • We are doing this so that when the user turned off test mode, it will update the primary database and the next request will use the primary database as per around_action logic

Here is my current code in application_controller.rb:

    around_action :setup_db_connection
    def setup_db_connection
      database_key = (user.test_mode?) ? :test_mode : :primary
      ActiveRecord::Base.connected_to(database: database_key) do
        yield
      end
    end

test_mode database key has test database configuration in database.yml and similarly for primary database key. Both are completely two different databases.

Can anyone please tell me if I am going in to correct direction? Any help or thought will be appriciated. Thank you in advance!!!!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You should just create an additional environment as this is the exact problem that they are designed to solve.

Rails just ships preconfigured with development, test and production as thats the bare minimum you can get by with. You can actually have an unlimeted number of environments.

So lets say you want to create an enviroment named staging which closely matches your production environment. You can start by copying the productions settings:

cp app/environments/production.rb app/environments/staging.rb 

And then just setup an additional hash in your database.yml and credentials files.

staging:
  <<: *default
  database: my_app_staging

You can then set the environment by using the RAILS_ENV env var or the command line arguments when starting the Rails server. Your staging environment can be run for example in a separate Heroku application made available on a subdomain. If you're using Heroku or some other SAAS platform a hobby tier will often be sufficient for staging.

When it comes to data you can either work on a set of dummy data generated by wiping and seeding or regularily wipe the staging database with a backup from your production database.

I would not consider using a "test mode switch" a good idea as it makes far to easy to inadvertantly mess things up. Using a separate environment lets you use a completely different set of credentials as well so you won't accentially do something like create a real credit card charge or destroy data on third party services. Sandboxes should be isolated.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!