如何检查Rails 3.2是否可以连接MySQL?

发布于 2025-01-08 12:28:32 字数 943 浏览 2 评论 0原文

是否有一种简单的“命令行/控制台”方法来检查 Rails 应用程序是否正确连接到 MySQL?

不幸的是,我无法启动 Rails 控制台。它抱怨:

/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>'.  

“abddadhocbkid”是第一个表的第一个属性,因此我认为它无法连接到 mysql 数据库。我需要找到一种方法来检查 Rails 是否可以在不登录 Rails 控制台的情况下连接到 mysql 数据库。

这是我的模型代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幸福丶如此 2025-01-15 12:28:32

如果您只需要检查连接建立后是否处于活动状态,可以使用 ActiveRecord::Base.connected? 方法。

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

抚你发端 2025-01-15 12:28:32

您可以尝试直接使用控制台连接:

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

或者如果您有模型,您可以尝试查找一个:

SomeModel.first

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文