4.5. Populating the Database with seeds.rb

With the file db/seeds.rb, the Rails gods have given us a way of feeding default values easily and quickly to a fresh installation. This is a normal Ruby program within the Rails environment. You have full access to all classes and methods of your application.
So you do not need to enter everything manually with rails console in order to make the records created in the section called “create” available in a new Rails application, but you can simply use the following file db/seeds.rb:
Country.create(name: 'Germany', population: 81831000)
Country.create(name: 'France', population: 65447374)
Country.create(name: 'Belgium', population: 10839905)
Country.create(name: 'Netherlands', population: 16680000)
You then populate it with data via rake db:seed. To be on the safe side, you should always set up the database from scratch with rake db:setup in the context of this book and then automatically populate it with the file db/seeds.rb. Here is what is looks like:
$ rake db:setup
db/development.sqlite3 already exists
-- create_table("countries", {:force=>true})
   -> 0.0101s
-- create_table("products", {:force=>true})
   -> 0.0030s
-- initialize_schema_migrations_table()
   -> 0.0010s
I use the file db/seeds.rb at this point because it offers a simple mechanism for filling an empty database with default values. In the course of this book, this will make it easier for us to set up quick example scenarios.

It's all just Ruby code

The db/seeds.rb is a Ruby program. Correspondingly, we can also use the following approach as an alternative:
country_list = [
  [ "Germany", 81831000 ],
  [ "France", 65447374 ],
  [ "Belgium", 10839905 ],
  [ "Netherlands", 16680000 ]

country_list.each do |name, population|
  Country.create( name: name, population: population )
The result is the same. I am showing you this example to make it clear that you can program completely normally within the file db/seeds.rb.

Generating seeds.rb From Existing Data

Sometimes it can be useful to export the current data pool of a Rails application into a db/seeds.rb. While writing this book, I encountered this problem in almost every chapter. Unfortunately, there is no standard approach for this. I am showing you what you can do in this case. There are other, more complex scenarios that can be derived from my approach.
We create our own little rake task for that. That can be done by creating the file lib/tasks/export.rake with the following content:
namespace :export do
  desc "Prints Country.all in a seeds.rb way."
  task :seeds_format => :environment do
    Country.order(:id).all.each do |country|
      puts "Country.create(#{country.serializable_hash.delete_if {|key, value| ['created_at','updated_at','id'].include?(key)}.to_s.gsub(/[{}]/,'')})"
Then you can call the corresponding rake task with the command rake export:seeds_format:
$ rake export:seeds_format
Country.create("name"=>"Germany", "population"=>81831000)
Country.create("name"=>"France", "population"=>65447374)
Country.create("name"=>"Belgium", "population"=>10839905)
Country.create("name"=>"Netherlands", "population"=>16680000)
You can either expand this program so that the output is written directly into the db/seeds.rb or you can simply use the shell:
$ rake export:seeds_format > db/seeds.rb