1

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

4 Answers 4

2

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.

1

The connection hasn't been established yet because it was not needed.

Try User.find(1) instead, and see if the connection is successful

1
  • Thanks you, You and PRAVEENKUMAR are correct. But I have not seen this behaviour before I have always seen like this. ( from a different project using Rails 4.0.3) main)> User class User < ActiveRecord::Base { :id => :integer, :name => :string, :lastname => :string, :email => :string, :password_digest => :string, :created_at => :datetime, :updated_at => :datetime }
    – lmcdougall
    Jun 26, 2015 at 22:46
0

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 dorake db:migrate or check schema file is generated or not and make connection atleast once before by asking any data.

1
  • The behavior I am expecting is an empty object listing all of its attributes. With I have seen before this incident.
    – lmcdougall
    Jun 26, 2015 at 22:40
-1

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.

1
  • 1
    There is nothing fishy going on. This is expected behavior. Whether he is using 4.2.1 or 4.2.3 is irrelevant. Jun 27, 2015 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.