What is the function of the seeds.rb file?

此生再无相见时 提交于 2019-11-30 09:53:07

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 -T in your project directory shows information about following tasks:

  • rake db:seed
    Load the seed data from db/seeds.rb
  • rake db:setup
    Create the database, load the schema, and initialize with the seed data
  • rake db:reset
    Same as rake db:setup, but drop the database first

So, you can run rake db:seed to run the seeds.rb file manually at any time. However, in most cases you will probably want to run rake db:setup or rake db:reset instead whenever you bootstrap your application.

The Purpose of seed.rb file is very simple, it allows us to accept data in our (Model of) database through writing in a file using a syntax and after rake task it populated as we entered this data through a Form using controller, models. For example:

Country.create(name: 'Germany', population: 81831000)
Country.create(name: 'France', population: 65447374)
Country.create(name: 'Belgium', population: 10839905)
Country.create(name: 'Netherlands', population: 16680000)

after that: rake db:seed rake db:setup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!