📜  rails 6 中的多个种子 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:50.136000             🧑  作者: Mango

代码示例1
# lib/tasks/custom_seed.rake
# lib/tasks/custom_seed.rake
namespace :db do
  namespace :seed do

    Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
      task_name = File.basename(filename, '.rb').intern

      task task_name => :environment do
        load(filename)
      end
    end

    task :all => :environment do
      Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].sort.each do |filename|
        load(filename)
      end
    end

  end
end

# Then, in order to run specific seed file, you can just run
rake db:seed:seed_file_name
#To run all the seeds file with order in that db/seeds folder, run below command
rake db:seed:all