0

I want to backtest some data, I would like the output of my backtest and analysis to be an input into the decisions of my application.

I thought about duplicating a model/table and for the purposes of performing my backtest and analysis, but then I would be doubling my workload and isn't particularly scalable.

Is it possible to dynamically toggle the rails environment for particular a particular class? I.e. create a BacktestingService class, any database read/writes are done to a 'test' database whilst the rest of the app continues to use development/production environment?

Can you suggest any other solutions for my use case?

Will
  • 521
  • 1
  • 6
  • 18
  • 1
    There is an article about [Multiple Databases with Active Record](https://edgeguides.rubyonrails.org/active_record_multiple_databases.html) in the Rails Guides. – spickermann Jan 01 '21 at 18:17
  • @spickermann If I read the docs correctly, "Using manual connection switching" fits my use case? – Will Jan 01 '21 at 18:29

1 Answers1

0

Using connected_to I can switch to a different database (in this case :test), perform my backtest and analysis within a connected_to block:

 ActiveRecord::Base.connected_to(database: :test) do
    backtest stuff here
 end

Outside of the block I can use my default database.

I found this useful: https://prathamesh.tech/2019/08/13/rails-6-multi-database-part-two/

Will
  • 521
  • 1
  • 6
  • 18