简体   繁体   English

Rails 4.2.1无法建立连接

[英]Rails 4.2.1 fails to establish a connection

I am using ruby 2.2.2 我正在使用ruby 2.2.2

Loading development environment (Rails 4.2.1)
irb(main):001:0> User
=> User (call 'User.connection' to establish a connection)
irb(main):002:0>

The application seems to be working properly. 该应用程序似乎正常运行。 I did insert <%= console %> and I was able to do debug in the browser. 我确实插入了<%= console %>并且能够在浏览器中进行调试。 I do not have a .irb-rails file (I thought I may have some funky things there). 我没有.irb-rails文件(我认为那里可能有一些时髦的东西)。

That was yesterday. 那是昨天。 Since then, I've upgrade rails to 4.2.3 made a new application 从那时起,我将rails升级到4.2.3并做了一个新应用

rails new blog
rails g scaffold blog title
rake db:migrate
rails c
irb(main)> Blog
Blog (call 'Blog.connection' to establish a connection)

Can someone tell me what is going on? 有人可以告诉我怎么回事吗? Please

The output is telling you exactly what is going on. 输出结果告诉您发生了什么。 Rails does not establish a connection to the database until it is needed. 在需要之前,Rails不会建立与数据库的连接。

This behavior has been part of Rails since the first releases of 4.x. 自从4.x的第一个发行版以来,此行为一直是Rails的一部分。 Here's a GitHub issue talking about it in relation to 4.0.1 这是一个关于4.0.1的GitHub问题

If you do what it tells you, and execute Blog.connection , it will connect to the database and you will see the column names in the output. 如果执行它告诉您的操作,然后执行Blog.connection ,它将连接到数据库,并且您将在输出中看到列名。

If this process is annoying to you, you can use the official API for seeing columns names instead: Blog.column_names . 如果这个过程很烦您,您可以使用官方API来查看列名: Blog.column_names

The connection hasn't been established yet because it was not needed. 由于尚未建立连接,因此尚未建立连接。

Try User.find(1) instead, and see if the connection is successful 改用User.find(1) ,看看连接是否成功

This make no sense when just call your model try to fetch some data like "Blog.all" this makes connection and fetch all data from blog. 当仅调用模型尝试获取诸如“ Blog.all”之类的数据时,这没有任何意义,这将建立连接并从Blog中获取所有数据。

This is an expected behaviour when You have'nt migrated your db. 当您尚未迁移数据库时,这是预期的行为。 Just do rake db:migrate or check schema file is generated or not and make connection atleast once before by asking any data. 只需执行rake db:migrate或检查是否生成了模式文件,并通过询问任何数据就至少建立一次连接。

Something fishy is going on in your environment. 您的环境中正在发生可疑的事情。 Here is my session that works: 这是我的工作原理:

zepho-mac-pro:blog zepho$ rails c
Loading development environment (Rails 4.2.3)
2.2.2 :001 > Article
  => Article (call 'Article.connection' to establish a connection) 
2.2.2 :002 > Article.count
(0.1ms)  SELECT COUNT(*) FROM "articles"
=> 0 

When you enter the rails console. 当您进入rails控制台时。 It should display Rails 4.2.3 as you see in my example. 如我的示例所示,它应该显示Rails 4.2.3。 Your session has Rails 4.2.1. 您的会话具有Rails 4.2.1。 Make sure that you use the right RVM gemset and Ruby version. 确保使用正确的RVM gemset和Ruby版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM