Rails connection switching issue

39 views Asked by At

I use connection switching in AA backend. Depending on session i switch to the archive db. The problem is that calling ActiveRecord::Base.connected_to with database key switches the connection forever, not only inside the block. And another queries outside of AA start to go to the archive db.

  # config/initializers/active_admin.rb
  config.around_action do |controller, action|
    if session['ARCHIVE'] == 'archive'
      ActiveRecord::Base.connected_to(database: :archive, prevent_writes: true) do
        action.call
      end
    else
      action.call
  end

Is it possible to handle it?

1

There are 1 answers

1
Rainy sidewalks On

i may be wrong ,is this is what you are looking for?

config.around_action do |controller, action|
  if session['ARCHIVE'] == 'archive'
    ActiveRecord::Base.establish_connection(:archive)
    action.call
    ActiveRecord::Base.establish_connection(:default)
  else
    action.call
  end
end