Rails C Not Working Properly on c9



Rails C Doesn’t Connect to the Right Database
Below is the issue I am having when I run rails c to see if my database is working.

ubuntu@seaanna-rails-app-4537161:~/workspace$ rails c

Running via Spring preloader in process 142252

Loading development environment (Rails 5.0.2)

2.3.0 :001 > ActiveRecord::Base.connection.instance_variable_get(:@config)

=> {:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>nil, :username=>nil, :password=>nil, :host=>nil, :port=>nil}



adapter: postgresql

encoding: unicode

pool: 5

database: <%= ENV['RDS_DB_NAME'] %>

username: <%= ENV['RDS_USERNAME'] %>

password: <%= ENV['RDS_PASSWORD'] %>

host: <%= ENV['RDS_HOSTNAME'] %>

port: <%= ENV['RDS_PORT'] %>

The env variables are defined in ~/.bashrc and ~/.profile

Even if I explicitly set the env variables, it still does not connect to the right database.


RDS_DB_NAME=foobar RDS_HOSTNAME=foobar RDS_PASSWORD=foobar RDS_PORT=foobar RDS_USERNAME=foobar rails c

It works fine in the rails runner. I ran the following command using better errors/ web console and get the correct output.


{:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"foobar", :username=>"foobar", :password=>"foobar", :host=>"foobar", :port=>foobar

I have to input my ENV variables every time before I do a migration etc. I have not had this problem before. Besides things not working correctly, is there a command or code to put in my project to not have to post the ENV variables before commands when they are already defined in the space.



You mentioned that the error happens on the command line only, but are you running the same command via runner as well as the command line? As the default runner, I think starts the rails server, not the rails console.



I fixed my issue. I was getting confused with the ENV variables being in the rails runner and NOT able to get into rails c to look at the database how I needed to.
I ended up making a .env file in my root directory and included it in my gitignore file. I was having to input my ENV variables IN the terminal every time I needed to migrate or do anything involving the db. I was just confused with the placement and storage of the variables. It would seem that they would be used across the server and into rails c but it was not doing that. If there is a better way in cloud9 please let me know. For now my solution is working :]

Thank You