开发者

Add seed data to development database for testing

开发者 https://www.devze.com 2023-04-12 09:20 出处:网络
I have a few tables that I would like to add about 10 rows of data to, in a manner that respects and illustrates their relationships.

I have a few tables that I would like to add about 10 rows of data to, in a manner that respects and illustrates their relationships.

  • How can I add seed data (dummy data) to my applications' development database for testing?

    I'm hoping some开发者_开发知识库one could point me to a rails friendly method for doing this.

  • Is there an easy way to make the CRUD methods in each table perspective controllers?


This is what the db/seeds.rb file is for.

You can execute it with rake db:seed

The default contents of seeds.rb

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Mayor.create(:name => 'Daley', :city => cities.first)

You can use ruby in this, so to insert 10 users:

1.upto(10) do |i|
   User.create(:name => "User #{i}")
end
0

精彩评论

暂无评论...
验证码 换一张
取 消