Blazer raised ActiveRecord::ConnectionNotEstablished

49 Views Asked by At

I have Blazer 3.0.0 running on my Rails 7 app with a multi-database config. I could create and save queries however, Blazer showed a flash error message:

ActiveRecord::ConnectionNotEstablished

when I tried to run any queries as shown below.

enter image description here

1

There are 1 best solutions below

0
On

I have to enforce Blazer to use a writing role DB connection.

# /apps/decorator/blazer/base_controller_decorator.rb
# load the *_decorator.rb in the application.rb

module Blazer
  module BaseControllerDecorator
    def self.prepended(base)
      base.around_action :set_writing_role
    end

    def set_writing_role(&)
      ActiveRecord::Base.connected_to(role: :writing, &)
    end
  end
end

unless Blazer::BaseController.ancestors.include?(Blazer::BaseControllerDecorator)
  Blazer::BaseController.prepend(Blazer::BaseControllerDecorator)
end

I think Blazer still uses the main Rails app db connection pool to save some Blazer's Blazer-related things in the DB. Running a query is a GET request and Rails will assign a reading role to the request causing Blazer to fail.