Skip to content

Instantly share code, notes, and snippets.

@RaffaeleSgarro
Created January 15, 2013 15:07
Show Gist options
  • Save RaffaeleSgarro/4539279 to your computer and use it in GitHub Desktop.
Save RaffaeleSgarro/4539279 to your computer and use it in GitHub Desktop.
Rake task to fill the database of a Rails app with fake data
# Run it with rake db:populate
namespace :db do
desc "Empty the db and fill it with fake data"
task :populate => :environment do
require 'faker'
Rake::Task['db:reset'].invoke
def time_rand from = 0.0, to = Time.now
Time.at(from + rand * (to.to_f - from.to_f))
end
10.times do
student = Student.create ({
:first_name => Faker::Name.first_name,
:last_name => Faker::Name.last_name,
:address => Faker::Address.street_address,
:birth => time_rand,
:phone => Faker::PhoneNumber.phone_number})
student.save!
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment