Generating Initial Database Data

While most data during development can be generated on the fly during your testing procedures, you may often find yourself in a situation where you need specific dummy data to fill up your database while you work and tweak your code or even data for the live environment. Luckily, the db/seeds.rb file exists exactly for this purpose.

Ruby on Rails database generation
Ruby on Rails seeds.gb

Since seeds.rb is of course a Ruby file, it can contain almost any Ruby code you need in order to create your initial data.  For example, our books project might add a couple books and authors like so:

Once you have some code in seeds.rb to generate data, you can populate your database from the terminal:

There are even great Ruby gems like the Faker gem for creating more realistic dummy data such as names, colors, and URLs.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *