Imagine you've finished building a feature with tests and want to play with it in the UI. You'll have to find a way to get data into your development database somehow. There are two ways of going about this:
Fire up
rails console
and manually create all the data needed. This is fine and works when you want to create one-off data entries. Overtime you'll find that things become cumbersome when you need to add more data. What about when you want to fill up a development database over a network? Or when you accidentally lose all your development data and need to repopulate? Things can get out of hand quickly.Populate your
seeds.rb
with all the data you need. This approach is your sure bet to get default values in your database in less time and is stress-free.
Example seed file:
1 2 3 4 |
|
After filling in all data you want to be created, you'll simply run the following command in your terminal:
1
|
|
That its all it takes. When things get out of hand, you can start on a clean slate by running this in your terminal:
1 2 3 4 |
|
This will first drop the development database, then re-create it, run all migrations and finally initialize with the seed data.
There is a simple rake
task to complish all this in one fell swoop:
1
|
|