Last active
February 1, 2018 20:40
-
-
Save jgv/4638785 to your computer and use it in GitHub Desktop.
Rake task to populate a database with data from factory girl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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