What is Rails Seed Command: rails db:seed
........................................................................................................................
Rails seed files are a useful way of populating a database with the initial data needed for a Rails project. The Rails db/seeds. rb file contains plain Ruby code and can be run with the Rails-default rails db:seed task.
........................................................................................................................
4.5. Populating the Database with seeds.rb
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.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)
db/seeds.rb. Here is what is looks like:$ rake db:setup
db/development.sqlite3 already exists
-- create_table("countries", {:force=>true})
-> 0.0175s
-- initialize_schema_migrations_table()
-> 0.0005s
-- assume_migrated_upto_version(20121114110230, ["/Users/xyz/sandbox/europe/db/migrate"])
-> 0.0006s
$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
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 ) end
db/seeds.rb.Generating seeds.rb From Existing Data
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.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(/[{}]/,'')})"
end
end
end$ 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)
$db/seeds.rb or you can simply use the shell:$ rake export:seeds_format > db/seeds.rb $
UTF-8
db/seeds.rb, then you need to enter the line# ruby encoding: utf-8
# ruby encoding: utf-8 Country.create(name: 'Germany', population: 81831000) Country.create(name: 'France', population: 65447374) Country.create(name: 'Belgium', population: 10839905) Country.create(name: 'Netherlands', population: 16680000) Country.create(name: 'Austria', population: 8440465) Country.create(name: 'Republika e Shqipërisë', population: 2831741)
Updates about this book will be published on my Twitter feed.
Comments
Post a Comment