Note that there are some explanatory texts on larger screens.

plurals
  1. POAt what point does ActiveRecord actually connect to the database?
    text
    copied!<p>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 <code>A</code> if it doesn't exist", "download and parse a file", "save parsed records to <code>A</code>".</p> <p>The code looks like the following:</p> <pre><code>ActiveRecord::Base.establish_connection( :adapter =&gt; 'mysql', :database =&gt; database_name, :username =&gt; username, :password =&gt; password, :host =&gt; "localhost", :port =&gt; 3306 ) ... ActiveRecord::Schema.define do create_table a, :force =&gt; true do |t| t.string :last_name, :limit =&gt; 60, :default =&gt; "", :null =&gt; false t.string :first_name, :limit =&gt; 30, :default =&gt; "", :null =&gt; false t.string :middle_initial, :limit =&gt; 2, :default =&gt; "" t.string :dob, :limit =&gt; 12, :default =&gt; "", :null =&gt; false end end unless A.table_exists? </code></pre> <p>However, if I put incorrect DB credentials, or a non-existent database name into the <code>establish_connection</code> 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 <code>A</code>). I tried a <code>begin-rescue-end</code> around <code>establish_connection</code>, but it never went into the <code>rescue</code> block.</p> <p>Why does <code>establish_connection</code> 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 <a href="http://api.rubyonrails.org/v2.3.8/classes/ActiveRecord/Base.html#M001778" rel="nofollow">HERE</a> sure don't seem to be any help.</p> <p>Or am I doing something wrong? Please help!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload