我已经找了一个星期了。我正在使用 Rails 3。
我的应用程序中有一个文档部分,我想用虚假数据填充数据库。到目前为止它非常简单:只需在 Fixture 文件夹中添加一个 .yml 文件即可。
现在我想要的是有一个 fixture 文件 (.yml),当我 rake db:fixtures:load
时,它会用文档填充数据库。我只是不知道如何实现这一目标。我一直在寻找 fixture_file_upload
函数,但我认为这不是可行的方法。
有什么想法吗?提前致谢。
编辑:明确地说,我想上传我在我的文档 fixture 中列出的文档。所以当我 rake db:fixtures:load
时,文件实际上被上传了。
最佳答案
通常加载种子数据是使用 rake db:seed
完成的,它只会执行您的 db/seed.rb
。在那里你可以做任何你想做的事。
如果你想重用你的装置,你可以手动加载它们:
require 'active_record/fixtures'
ActiveRecord::Fixtures.create_fixtures(Rails.root.join('test/fixtures'),
'your_yml_file')
关于ruby-on-rails - rails : upload a file in a fixture to populate the db (seed data),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17053155/