Adding a custom seed file

Ruby on-RailsRubyRuby on-Rails-3.2RakeSeed

Ruby on-Rails Problem Overview


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 the default seeds.rb file, you run the command rake db:seed.

If I create a file in the db directory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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/tasks directory:

# 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/seeds directory (excluding the .rb extension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:

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 --tasks or rake -T.

Solution 2 - Ruby on-Rails

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=<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).

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

Solution 3 - Ruby on-Rails

Too complicated! I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.

# 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFellow StrangerView Question on Stackoverflow
Solution 1 - Ruby on-RailszeantsoiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsHeath NView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAaron HendersonView Answer on Stackoverflow