No database connection in rails console
simple solution:
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.
User.count
That fixed the false positive warning for me and a colleague.
or
Use Model.connection
to establish a connection.
Rails console can't connect to database
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.
Rails console can't connect to database but rake tasks can
It turns out that the important difference is the Spring preloader. It seems that you've started Spring before you've defined the ENV variable.
Try killing Spring and restarting the console.
rails console won't connect to database
I have found out the fix - but I am not sure why this is a problem.. but.. rails doesn't like having a project in a folder named rails!
My rails project was in a folder:
/var/deployed/rails
when I rename it to:
/var/deployed/myapp
Then $ rails console works perfectly to talk to the db.
Weird!
How to find database name in rails console
This works in Rails 3 and Rails 4
ActiveRecord::Base.connection.current_database
But this only works with drivers that have implemented that method. For example, it will not work for SQLite but will work with mysql and postgres.
Unable to connect to database from rails console on dockers
After trying alot at last I found a solution. I was using docker run -i -t project_app:latest bash
command to access container
and to run rails console
command which was wrong.
The correct command to access container
's terminal is
docker exec -it project_app /bin/bash
After accessing terminal
I was able to successfully run query
i.e User.first
inside rails console
.
How can I switch database inside a Rails console on Heroku?
The problem was that using url:
didn't work and all parameters needed to be specified.
config = {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>10, "username"=>"u...", "password"=>"p...", "port"=>5432, "database"=>"d...", "host"=>"ec2..."}
If you go for a 3tier database yml, you can use this:
config = ActiveRecord::Base.configurations["production"]["seconddb"]
Then, you can use establish_connection
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute("select count(*) from users")
Once I started specifying username, password, port, database and host, it all worked like a charm.
To work with both databases at the same time, a good way is to create a class
class OtherDB < ActiveRecord::Base
establish_connection(ActiveRecord::Base.configurations["production"]["seconddb"])
end
Then you can call things that way
OtherDB.table_name = "table_name"
OtherDB.first
ref (Establish a connection to another database only in a block?)
And to run SQL commands:
OtherDB.connection.execute("select count(*) from users")
Related Topics
How to Run a Ruby Script Within Bundler Context
Bundle Install Could Not Fetch Specs from Https://Rubygems.Org/
How to Force Ruby to Show a Full Stack Trace
How to Add Two Weeks to Time.Now
How to Remove Validation Using Instance_Eval Clause in Rails
Convert JSON String to JSON Array in Rails
Calling/Applying Lambda VS. Function Call - the Syntax in Ruby Is Different. Why
Cleanest Way to Create a Hash from an Array
Code to Generate Gaussian (Normally Distributed) Random Numbers in Ruby
How to Put Assertions in Ruby Code
Iterate Every Month with Date Objects
How to Get Request.Uri in Model in Rails
Creating Categories on Jekyll Driven Site
How to Read the Body Text of an Email Using Ruby's Net/Imap Library