Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

328
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda