Skip to content

Instantly share code, notes, and snippets.

@jgv
Last active February 1, 2018 20:40
Show Gist options
  • Save jgv/4638785 to your computer and use it in GitHub Desktop.
Save jgv/4638785 to your computer and use it in GitHub Desktop.
Rake task to populate a database with data from factory girl.
require 'factory_girl'
namespace :db do
desc "Populate the database with some sample data"
task :populate, [:count] => [:environment] do |t, args|
args.with_defaults(:count => 10)
puts "Resetting the database"
Rake::Task['db:reset'].invoke
puts "Creating #{args[:count]} users"
new_users = FactoryGirl.create_list(:user, args[:count].to_i)
puts "Creating a product for each user"
new_users.each_with_index do |u|
FactoryGirl.create(:product, :user_id => u.id)
end
puts "Done!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment