Rails confused between dev and test environment - ruby-on-rails

I'm just moving my rails app to a new development machine.
for the first time, I am running rails on ubuntu in a virtualbox on a windows 7 host OS.
When I run rake db:create, it creates the rails_app_test.
When I run rails s, I get the error
unknown database rails_app_dev
I am trying to use the development environment, but for some reason rake is creating test. Why is this, and how do i correct it?

Your environment variable seems to equal 'test'.
Try:
rake db:migrate RAILS_ENV=development

Related

Error when running bin/rails test in Vagrant

I'm running the rails-dev-box on Vagrant, with a folder shared between the box and my Windows computer. Rails version 5.0.5. I have a very basic app using a sqlite3 database, and a basic generated scaffold for a model. When I ran bin/rails test I received this error:
ActiveRecord::Tasks::DatabaseAlreadyExists
A link in this GitHub thread pointed to this SO question, and I followed this answer - I edited database.yml to change the location of the databases to a location outside of the shared folder. I then re-migrated the databases with bin/rails db:migrate. This seemed to help a bit, because the next time I ran bin/rails test I received a different error:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=test
But even after running bin/rails db:migrate RAILS_ENV=test I still receive this error each time I try to run the test.
I think all you need is bin/rails db:test:prepare before bin/rails test

How to run 'rake db:drop' in Elastic Beanstalk

I have a rails app running on the Elastic Beanstalk service.
When I deploy my app, some pages didn't work. I think the problem is that after I delete a model and create a new model with the same name but different schema, the database didn't reset.
On local, I can run rake db:drop, rake db:create, rake db:migrate to reset the database. However, how to reset the database on EB?
to run it manually, you can simply eb ssh your environment and issue the rake commands. Alternatively if you don't need that db, simply recreate the environment from the beanstalk web console, which will recreate the db.
i think you have to delete also structure.rb file manual from your rails application and let it create again by using rake db:migrate

rails console hangs in production environment

I have postgresql setup for my production schema in database.yml. I was able to successfully run RAILS_ENV=production rake db:migrate but when I do do RAILS_ENV=production rails console, it hangs. Never gives me the console prompt.
I've also tried bundle exec rails console production but didn't help.
Output:
Connecting to database specified by database.yml
...hangs after this.
Running rails 3.2.11 on ruby 2.1.2. I have rbenv and rbenv-gemsets installed.
To run the rails console in the production environment you need to pass the name of the environment after the command. The actual command is as follows.
rails console production
If that doesn't work, try restarting your PostgreSQL server and try again. It could be that you have crossed the number of possible connections with the PostgreSQL database.

running rake task on 'production' and specifying environment?

I have a host at Linode and am trying to run a Rake task on it, but I get a mySQL error saying it can't connect. It looks like it thinks it is on dev. I did some Googling and saw that I can do something like this:
bundle exec rails c
It loads the dev environment and I can't run User.all giving me an access denied error.
If I run bundle exec rails c RAILS_ENV=production I get the error:
Rails.env=production database is not configured (ActiveRecord::AdapterNotSpecified)
However, if I access it via the web, everything is OK. I was able to run rake db:seed before so I know that there's some way around this.
Accessing mySQL with the production credentials works fine.
Any ideas?
Try this:
rails c production
or, at the beginning:
RAILS_ENV=production rails c
It thinks you're passing RAILS_ENV=production as an argument when you put it at the end.
If you want to run your console in the context of the current bundle in your Gemfile and ensure you're using your Gemset use:
bundle exec rails c production
This works for me. It depends on how your server and all its dependencies are set up:
RAILS_ENV=production bundle exec rails console

rails3 app ENV not being recognized as production

I set up my rails app on my linode VPS, phusion passenger is installed and working, so is mysql (I know this cause my friend currently is running 2 production apps on it with the same set up). The VPS is running Ubuntu 10.10 and I'm using apache2 with passenger.
I SFTP'd the app to the server, bundle updated, set up my virtual host and specified RailsEnv to be production and paths are all accurate.
I then restarted the server (as root) with
apachectl -k restart
tried to rake db:migrate and it didn't do anything, so I figured it was because the environment didn't get changed, which is exactly what happend. If I go into the rails console and type Rails.env it gives me development.
I have no idea why, I did everything that should set it to production? Anyone know what I may have missed? Is there somewhere in the app where I'm supposed to change something to say production environment? I thought that only had to be done in rails 2.x
Thanks in advance for any and all help.
The RailsEnv setting is only for Passenger's use. It doesn't affect the commands you type in the shell.
Use
RAILS_ENV=production rake db:migrate
and
RAILS_ENV=production rails console
Or set the RAILS_ENV environment variable in your login shell to production so that you don't have to append RAILS_ENV=production to the commands you issue:
export RAILS_ENV=production
(exact command may vary depending on which shell you use; the above works in bash)
You were on the right track; all you need to do to actually run the app in production mode is set the RailsEnv as you did, assuming you are running the app using Passenger. However, to run the database migrations you need to tell Rails what environment to run within.
The rails console command defaults to the 'development' environment by default. The same goes for running database migrations.
To run the migrations on your production environment you need to run the command as such:
RAILS_ENV=production rake db:migrate
And to run the console in production mode, you need to run the console using the command:
rails console production
If you want this variable to be set automatically, put RAILS_ENV=productionat the end of your ~/.bashrc file. (This only works with a bash terminal)
Then open a new terminal, or restart your ssh connection.

Resources