将种子数据添加到开发数据库中进行测试

发布于 12-09 15:41 字数 197 浏览 5 评论 0原文

我有几个表,我想以尊重和说明它们关系的方式向其中添加大约 10 行数据。

  • 如何将种子数据(虚拟数据)添加到我的应用程序的开发数据库中以进行测试?
    我希望有人能给我指出一种对 Rails 友好的方法来做到这一点。

  • 是否有一种简单的方法可以在每个表透视控制器中创建 CRUD 方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香橙ぽ2024-12-16 15:41:59

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

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

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)

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

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文