Rails 4.2.1 fails to establish a connection - ruby-on-rails

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 dorake 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.

Related

ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base with mongoid

I have a Rails 4.2.8 app, ruby 2.4.4 with a Mongo database, using the mongoid ODB.
I want to run tests against this application using rspec. The most basic test gives me this error:
ActiveRecord::ConnectionNotEstablished: No connection pool for ActiveRecord::Base
My research on this problem lead me to these conclusions, but I do not know how to solve the problem itself:
why is active record mentionned here, when it is not in my gemfile ? Maybe another gem is loading it at some point, but how can i get rid of this problem ?
my application.yml and mongoid.yml file are correctly configured. I can RAILS_ENV=TEST rails console and I can query documents.
looks like rails is going inside the condition line 15 in this file. So how and why is ActiveRecord loaded at some point ?
Some help would really be appreciated as I do not know what to do next...
Ok so I did not figure out who was loading ActiveRecord in my application, but I manually unload it in my rails_helper.rb:
Object.send(:remove_const, :ActiveRecord)
# Make sure it is above this
require 'rspec/rails'
I ran into the same issue and found that the problem was database cleaner. Previously we were using the gem database_cleaner but that gem seems to have been split based on your database adapter. Switching to database_cleaner-mongoid resolved the issue. Note that you may need to change some requires or config values to get things running correctly after the change. For instance I had an error about my strategy not being supported in mongoid.

Method for reseting all users in database with Ruby on Rails 4

So I've set up a new authentication process within my website using rails (rails version '4.2.0', sqlite3 database manager, ruby 2.1.1p76) on OSX Mavericks (yes, I still haven't updated yet!). The website as yet is still only local as I'm learning rails.
I have modified the user authentication to utilise a cookies remember me function, but old users can not login, i.e. error Couldn't find User (ActiveRecord::RecordNotFound in WelcomeController#homepage)
Newly created users can.
I was just wondering what the terminal command is to somehow to delete all records of users from the database, so that all new users need to sign up to obtain the remember me functionality...? I can't seem to find the exact answer and I don't want to start messing too much with the database.
Start a rails console by typing
rails c
in the terminal, in your rails application folder (not the app subfolder).
type
User.destroy_all

Heroku throws error where local host does not

I have a rails app running on heroku. Locally everything works fine. However, upon pushing to heroku there is an error.
The logs state this:
app/controllers/orders_controller.rb:7:in `create'
ActiveRecord::UnknownAttributeError (unknown attribute: ammo_id):
Started POST "/orders?ammo_id=4" for 96.235.177.110 at 2014-08-09 00:03:09 +0000
I.e. that this is an error in my orders_controller on line 7. This is line 7:
#order = #cart.orders.build(ammo_id:params[:ammo_id])
Locally this run perfectly. However, on heroku it does not. I changed that line to:
#order = #cart.orders.build(:ammo_id => params[:ammo_id])
And now it works on heroku. So my question is, why does the first syntax not work on heroku? Does it have to do with versions?
Attributes
Having just answered another question like this, I'd say the problem is almost certainly to do with your Heroku DB not having the attributes it needs to run
The issue here is that your Rails development DB & Heroku production DB will be completely different, and as such, you need to make sure you have migrated your Heroku db as you have your local one
The way to fix it will be to run the following on your local machine:
$ heroku run rake db:migrate
--
Whenever Rails throws ActiveRecord::UnknownAttributeError - it means Rails does not have access to the particular attribute on your model class.
To understand this, you must remember that Rails is basically a series of classes which load with each request. These classes, as per the object-orientated structure, are populated by a series of attributes. These attributes, in the case of Rails, are pulled from the database -- meaning if your application cannot find specific attributes, it simply means the database doesn't have them
To fix this, you have to ensure you have the required columns present in your db. The most common instance of this issue is a lack of foreign_key in your various models
It might have to do with Database Migrations. Make sure you run heroku run rake db:migrate <your branch stuff here> after every push so that our schema updates to your new models.
Also,Your heroku may be defaulting to an older version of ruby. The => syntax for hashes was the default in older versions of ruby. But now {key: value} is a new acceptable format. Though this is very unlikely.

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.
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.
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
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.
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.
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?)

How do I upgrade from Ruby 1.8.7 to Ruby 1.9.2 for Rails 2.3.8 app?

I have searched all over and cannot find anyone with the same problem.
I am testing upgrading a large Rails app running Rails 2.3.8 with Ruby 1.9.2 using rvm and nginx+passenger (currently running on 1.8.7 in production).
I'm using the mysql2 gem, and my database uses UTF-8 encoding.
When I login to the app (standard restful-auth), the db lookup finds a match but when the controller tries to save an attribute change on the User model, it raises a validation error from validates_length_of.
I added some debugging output to ActiveRecord and it looks like it thinks all my database record's string values are empty (show up as "[]" in the logs...)
I was seeing the same behavior in script/console: User.first.login => "[]", but it was fixed after switching to the mysql2 gem (and updating database.yml adapter value).
Why would the console work but not via the website? ActionController / ActiveSupport bug??
Clarification: This is a stable app that has been live for 1+ years in 1.8.7 with hundreds of specs.
The solution was to disable the xss_terminate plugin.
It is old but was quite useful for me to deal with various user input types and roles..
Obviously hooks way to close to ActiveRecord operations dealing with strings to be useful in Ruby 1.9.

Resources