A Rake Task for Faker

Need to insert some fake data in your development database? Grab the Faker gem and drop this into lib/tasks/fake_data.rb. Customize it for your model(s) and execute with rake db:development:fake_data.

namespace :db do
  namespace :development do
    desc "Create records in the development database."
    task :fake_data => :environment do
      require 'faker'
 
      100.times do
        c = Client.create(
          :first_name => Faker::Name.first_name,
          :last_name => Faker::Name.last_name,
          :middle_initial => ("A".."Z").to_a.rand, 
          :ss_number => 99999999 + rand(999999999 - 99999999), 
          :date_of_birth => Time.now - (rand(12000)).days) 
      end
    end
  end
end

I’m sure there’s more optimization to be done, but it works for me!

UPDATE: I added a few extra lines for data outside of faker.

Leave a Reply

You must be logged in to post a comment.