Adding a custom seed file

后端 未结 3 1735
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 12:44

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.

To run

3条回答
  •  -上瘾入骨i
    2020-12-12 13:13

    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.

    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=
    

    This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).

    Working example:

    rake db:seed:single SEED=my_custom_seed
    

    The above would seed the Rails.root/db/seeds/my_custom_seed.seeds.rb file

提交回复
热议问题