I am very new to Ruby on Rails. I don’t know what the function of the seed.rb file is, why we use this file, and why we need to run the rake db:seed command. Can anyone give an explanation with examples?
I am very new to Ruby on Rails. I don’t know what the function
Share
Bootstrapping Data
The purpose of seed data is to bootstrap your database. For example, if you have a users table where you track users’ city and state, you may want to seed a related table with U.S. state names and abbreviations before creating the first user.
Likewise, you may also want to seed things like administrative accounts, or other data that’s necessary to run your application for the first time. As a general rule, you shouldn’t add anything to a seeds.rb file that isn’t necessary to bootstrap your database or its relations.
Related Rake Tasks
The seeds.rb file is where the seed data is stored, but you need to run the appropriate rake task to actually use the seed data. Using
rake -Tin your project directory shows information about following tasks:Load the seed data from db/seeds.rb
Create the database, load the schema, and initialize with the seed data
Same as
rake db:setup, but drop the database firstSo, you can run
rake db:seedto run the seeds.rb file manually at any time. However, in most cases you will probably want to runrake db:setuporrake db:resetinstead whenever you bootstrap your application.