Ruby-on-rails 添加自定义种子文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19872271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a custom seed file
提问by Fellow Stranger
I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.
我想用虚拟数据填充新功能,但不想使用 db/seeds.rb 文件,因为它已经有与此功能无关的其他数据的种子。
To run the default seeds.rbfile, you run the command rake db:seed.
要运行默认seeds.rb文件,请运行命令rake db:seed.
If I create a file in the dbdirectory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?
如果我在db名为的目录中创建一个文件seeds_feature_x.rb,那么 rake 命令会如何运行(仅)该文件?
回答by zeantsoi
Start by creating a separate directory to hold your custom seeds – this example uses db/seeds. Then, create a custom task by adding a rakefile to your lib/tasksdirectory:
首先创建一个单独的目录来保存您的自定义种子——这个例子使用db/seeds. 然后,通过将 rakefile 添加到您的lib/tasks目录来创建自定义任务:
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " + task_name + ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
This rakefile accepts the name of a seed file in the db/seedsdirectory (excluding the .rbextension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:
这个 rakefile 接受db/seeds目录中种子文件的名称(不包括.rb扩展名),然后像运行一样运行它seeds.rb。您可以通过从命令行发出以下命令来执行 rake 任务:
rake db:seed:file_name # Name of the file EXCLUDING the .rb extension
Update: Now it should also list the seed tasks when running rake --tasksor rake -T.
更新:现在它也应该在运行rake --tasks或时列出种子任务rake -T。
回答by Heath N
I tried out zeantsoi's answer but it didn't give me what I wanted, it did all files in a directory. Hacked away at it and got this.
我尝试了 zeantsoi 的答案,但它没有给我我想要的,它完成了一个目录中的所有文件。破解它并得到了这个。
namespace :db do
namespace :seed do
task :single => :environment do
filename = Dir[File.join(Rails.root, 'db', 'seeds', "#{ENV['SEED']}.seeds.rb")][0]
puts "Seeding #{filename}..."
load(filename) if File.exist?(filename)
end
end
end
And to use this do the following:
要使用它,请执行以下操作:
rake db:seed:single SEED=<seed_name_without_.seeds.rb>
This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).
这将在 Rails.root/db/seeds 文件夹中查找没有 .seeds.rb 的文件名(它会为您添加那些)。
Working example:
工作示例:
rake db:seed:single SEED=my_custom_seed
The above would seed the Rails.root/db/seeds/my_custom_seed.seeds.rbfile
以上将种子Rails.root/db/seeds/my_custom_seed.seeds.rb文件
回答by Aaron Henderson
Too complicated!I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.
太复杂!我只是想要一个简单的任务来执行 db/seeds 目录下的每个文件,而不传递任何文件名。
# lib/tasks/seed.rake
desc "Run all files in db/seeds directory"
namespace :db do
task seed: :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
puts "seeding - #{filename}. for reals, yo!"
load(filename)
end
end
end

