简体   繁体   中英

Rails 4.2.1 fails to establish a connection

I am using 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. I do not have a .irb-rails file (I thought I may have some funky things there).

That was yesterday. Since then, I've upgrade rails to 4.2.3 made a new application

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.

This behavior has been part of Rails since the first releases of 4.x. Here's a GitHub issue talking about it in relation to 4.0.1

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.

If this process is annoying to you, you can use the official API for seeing columns names instead: 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

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.

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.

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. It should display Rails 4.2.3 as you see in my example. Your session has Rails 4.2.1. Make sure that you use the right RVM gemset and Ruby version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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