Add seed data to development database for testing
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
精彩评论