Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Rails 3.2 can connect to MySQL?

Is there an easy 'command line/console' way of checking if a Rails app is connecting properly to MySQL?

Unfortunately, I can't get a Rails console to come up. It complains about:

/home/nexargi/www/gi/vendor/ruby/1.9.1/gems/activerecord-3.2.1/lib
  /active_record/dynamic_matchers.rb:50:in `method_missing': undefined
 local variable or method `abddadhocbkgid' for #<Class:0x000000036427f8> (NameError)
    from /home/nexargi/www/gi/app/models/adhoc_bkg_diners_diet.rb:5:in 
`<class:AdhocBkgDinersDiet>'.  

The 'abddadhocbkid' is the first attribute of the first table and therefore I am thinking that it is not managing to connect to the mysql database. I need to find a way of checking if Rails can connect to the mysql database without logging into the rails console.

Here is my model code:

class AdhocBkgDinersDiet < ActiveRecord::Base
  validates_presence_of :abddadhocbkgid, :abddmealdietid, :abddadultnos, :abddchildnos
  validates_numericality_of :abddadultnos, :abddchildnos, greater_than_or_equal_to: 0

  belongs_to :adhoc_bkg, foreign_key:abddadhocbkgid
 belongs_to :meal_diet, foreign_key: :abddmealdietid
end
like image 649
nexar Avatar asked Dec 01 '22 00:12

nexar


2 Answers

If you just need to check if your connection is active after it has been established, there is the ActiveRecord::Base.connected? method.

like image 135
bhamby Avatar answered Dec 06 '22 23:12

bhamby


You can try connecting directly using console:

ActiveRecord::Base.establish_connection(
  Rails.configuration.database_configuration[Rails.env]
)

Or if you have a model you can try finding one:

SomeModel.first
like image 40
PinnyM Avatar answered Dec 06 '22 23:12

PinnyM