Created
January 15, 2013 15:07
-
-
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
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
# 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