4

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 someone 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?

4

1 回答 1

12

这就是db/seeds.rb文件的用途。

您可以使用 rake db:seed 执行它

seed.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)

您可以在其中使用 ruby​​,因此要插入 10 个用户:

1.upto(10) do |i|
   User.create(:name => "User #{i}")
end
于 2011-10-12T18:50:22.190 回答