Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8045741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:38:00+00:00 2026-06-05T05:38:00+00:00

I have a ruby script that is using ActiveRecord (2.3.12) to access a MySQL

  • 0

I have a ruby script that is using ActiveRecord (2.3.12) to access a MySQL database. The flow goes something like, “read database values from a config file”, “connect to database”, “Create table A if it doesn’t exist”, “download and parse a file”, “save parsed records to A“.

The code looks like the following:

ActiveRecord::Base.establish_connection(
  :adapter => 'mysql',
  :database => database_name,
  :username => username,
  :password => password,
  :host => "localhost",
  :port => 3306
)

  ...

ActiveRecord::Schema.define do
  create_table a, :force => true do |t|
    t.string :last_name, :limit => 60, :default => "", :null => false
    t.string :first_name, :limit => 30, :default => "", :null => false
    t.string :middle_initial, :limit => 2, :default => ""
    t.string :dob, :limit => 12, :default => "", :null => false
  end
end unless A.table_exists?

However, if I put incorrect DB credentials, or a non-existent database name into the establish_connection method, the script doesn’t seem to give any errors or throw any exceptions until I actually try to perform some operation on the database (i.e., create table A). I tried a begin-rescue-end around establish_connection, but it never went into the rescue block.

Why does establish_connection seem to not really…well…establish the connection? And for the life of me, I can’t figure out what it is even supposed to return. The docs HERE sure don’t seem to be any help.

Or am I doing something wrong? Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:38:02+00:00Added an answer on June 5, 2026 at 5:38 am

    I usually use ActiveRecord::Base.connection.active? statement to check the whether the ActiveRecord is really connected to database.

    def establish_database_connection
      begin
        ActiveRecord::Base.establish_connection config["database"]
        ActiveRecord::Base.connection.active?
        logger.info "Connected to Database"
      rescue Exception => e
        logger.error "Exception db connection :  #{e.message} "
        raise "Database connection failed"
      end
    end
    

    Without ActiveRecord::Base.connection.active? statement the above code won’t raise any error on invalid credentials.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of scripts that interact with a MySQL database using ActiveRecord
I have a small ruby script in which I'd like to use ActiveRecord to
I have a simple Ruby script that looks like this require 'csv' while line
I have a Ruby script that produces a Latex document using an erb template.
I have written a ruby script to screen scrape something using the 'open-uri' and
I have a ruby script that creates a message using AMQP in RabbitMQ. #
so i have ruby script that simply writes to a test.txt file with hello
I have a ruby script that does a few perforce operations (through the scripting
I have a ruby script that is simultaneously and asynchronously receiving and displaying messages
I have this ruby script that allows me to enter the name of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.