First create folder db/seeds
Then, create a custom task by adding a rakefile to your lib/tasks
directory
1 2 3 4 5 6 7 8 9 10 11 |
# 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 |
You can execute the rake task by issuing the following from the command line:
1 |
rake db:seed:file_name # Name of the file EXCLUDING the .rb extension |