3

I've been looking for a week now. I'm using Rails 3.

I have a document section in my app and I'd like to populate the database with fake data. So far it was super easy: just add a .yml file in the Fixture folder and that's it.

Now what I'd like is to have a fixture file (.yml) that would populate the DB with documents when I rake db:fixtures:load. I just don't know how to achieve this. I've been looking to the fixture_file_upload function but I don't think it's the way to go.

Any idea? Thanks in advance.

Edit: to be perfectly clear, I'd like to upload a document that I list in my Document fixture. So when I rake db:fixtures:load, the document is actually uploaded.

4

3 回答 3

5

通常加载种子数据是使用 rake db:seed它来完成的,它只执行你的db/seed.rb. 在那里你可以做任何你想做的事。

如果您想重用您的固定装置,您可以手动加载它们:

require 'active_record/fixtures'
ActiveRecord::Fixtures.create_fixtures(Rails.root.join('test/fixtures'), 
                                       'your_yml_file')
于 2013-06-11T21:03:58.377 回答
1

传递 FIXTURES 环境变量

假设你有test/fixtures/documents.yml并且你不介意破坏文档表中当前的数据,你可以加载你的夹具:

rake db:fixtures:load FIXTURES=documents

如果需要,您还可以传递适当的 RAILS_ENV 以将数据加载到开发数据库以外的其他地方,例如测试或生产。不过,请确保先备份数据库。一个错字可能会清除您当前的数据集。

于 2013-06-11T20:37:59.753 回答
0

您是在为测试安装夹具数据,还是使用测试/夹具来加载虚假的开发数据?如果是前者,那就很难了,因为您不希望测试运行程序在每次测试之间上传一堆文件。如果是后者,那么您应该改用 rake db:seeds。它在 Rails 应用程序的上下文中运行 db/seeds.rb。使用环境标志仅在开发中加载假数据。然后,您可以使用附件库的 API 来“上传”文件。

于 2013-06-11T20:57:13.870 回答