Wordica
Wordica

Reputation: 2595

Rails console can't connect to database

In last days i update my OS X to Maverics. Today when i try to create new project like this:

rails new abc

there were many problems but i install xcode and now it's work. Right now i open rails console like this:

rails console

and then whatever i write i only see:

Loading development environment (Rails 4.0.1)
1.9.3p448 :001 > Link
=> Link(no database connection)

What is wrong? Mysql is running, database exist. When i do rake db:migrate everything works fine.

Upvotes: 27

Views: 9573

Answers (5)

Koray Güclü
Koray Güclü

Reputation: 2896

Try using reload! on the console and ensure that you have records in the specified model if not create records for the relations etc..

I had the same problem on ubuntu. I used reload! in rails console and added records in the database.

Upvotes: 4

Joseph Ravenwolfe
Joseph Ravenwolfe

Reputation: 6600

In the past, referencing an ActiveRecord model immediately after loading the console would return its column information. Rails now only returns this information if it has been explicitly coerced to connect to the database by a method like connection, first, all, count, etc.

You can restore the previous behavior by adding this to your config/application.rb

console do
  ActiveRecord::Base.connection
end

Upvotes: 29

Juanito Fatas
Juanito Fatas

Reputation: 9969

According to the issues #12804 on rails/rails.

The message just tells you that AR has not yet connected to the database and therefore does not know the column information. I think it's desired not to connect to the database unless we need to fetch data.

For now you could use Model.connection to establish a connection.

Upvotes: 19

barelyknown
barelyknown

Reputation: 5570

The console probably does have a database connection but is reporting that it doesn't.

To see if that's true, make a query in the console.

Link.count

That fixed the false positive warning for me and a colleague.

Upvotes: 60

Jon Evans
Jon Evans

Reputation: 189

I'm having the same issue with Rails 4.0.1. It's occurring on the Linux server I'm deploying to as well as my Mavericks development machine.

The server works, specs work, but the console doesn't have a database connection.

Reverting to Rails 4.0.0 fixes the issue with the console.

I haven't found any other mention of this issue. There's probably an issue with the changes for 4.0.1 and the Postgres adapter, maybe? (Are you using Postgres?)

Upvotes: 3

Related Questions