Ruby - check if database connection is possible

I am looking for a way to test the database connection for our application.

We are currently having problems connecting, so we want to test the connectivity without having to log in.

Is there a way to show the page with the message about connecting the database up, or is the connection to the database independent of whether the connection will occur. If any further information is needed, please let me know.

Just start to learn the ropes, so apologize if I lack details.

+4
source share
1 answer

You can check if a connection is possible by running the following script:

require './config/environment.rb' # Assuming the script is located in the root of the rails app
begin
  ActiveRecord::Base.establish_connection # Establishes connection
  ActiveRecord::Base.connection # Calls connection object
  puts "CONNECTED!" if ActiveRecord::Base.connected? 
  puts "NOT CONNECTED!" unless ActiveRecord::Base.connected?
rescue
  puts "NOT CONNECTED!"
end
+10
source

All Articles