Resetting the database in Rails 4
So I made a new branch in git to continue my rails study as suggested in Michael Hartl’s 3rd edition. I used rails console to create a new user so that I could setup the user page (at this time we did not have the sign up form). So per instructions it says
Since we’re about to add the ability to create new users through the
web, let’s remove the user created at the console in Section 6.3.4.
The cleanest way to do this is to reset the database with
bundle exec rake db:migrate:reset
- An error occurred while installing sqlite3 (1.3.11), and Bundler cannot continue
- Subclipse error “svn: SQLite compiled for 3.8.6, but running with 3.8.5”
- Not able to create staging instance on heroku
- Merging two branches, one with Postgres and another with SQLite3
- Rails: Populate Heroku database with development.sqlite3 data
- Rails app using sqlite3 for development won't deploy into heroku
I was under the impression that
db:reset is suppose to dump the current database and rebuild it from the schema.rb, but for me it deletes everything in my schema leaving it blank and I have to copy an earlier version of my schema from another branch.
Is there something I’m not doing correctly or overlooking? I thought I understood the db::xxxx commands but apparently not as well as I fooled myself into thinking ha.
One Solution collect form web for “Resetting the database in Rails 4”
Nope, this is the expected behavior. The test data resets every time you run the tests unless you provide seed date. You should check out Factory Girl to build objects for your test database: https://github.com/thoughtbot/factory_girl
Hartl covers Factory Girl later in the book/tutorial, so you might not have gotten to that part yet.
So you are partially correct; rake db:reset will rebuild your schema, but all the data will be destroyed. It’s a bad idea to use a copy of an existing database for testing purposes.
To rebuild the schema I usually run:
Works every time.