I'm currently working on a project to create a travel website, and I needed to have a bunch of member profiles to use for testing the member directory. I came across a great Ruby gem called Faker which I've used to create the dummy accounts.

Installation is easy, of course: gem install Faker

Next, I created a Rake task for populating the accounts:

namespace :db do
  namespace :development do
    desc "Create user records in the development database."

    task :fake_user_data => :environment do
      require 'faker'

      @countries = ["United States", "Canada", "United Kingdom", "Germany", "Mexico"]
      @genders = ["Male","Female"]
      @privacy = ["members", "public"]

      def randomDate(params={})
        years_back = params[:year_range] || 5
        latest_year  = params [:year_latest] || 0
        year = (rand * (years_back)).ceil + (Time.now.year - latest_year - years_back)
        month = (rand * 12).ceil
        day = (rand * 31).ceil
        series = [date = Time.local(year, month, day)]
        if params[:series]
          params[:series].each do |some_time_after|
            series << series.last + (rand * some_time_after).ceil
          end
          return series
        end
        date
      end

      100.times do

        u = User.new(
          :first_name => Faker::Name.first_name,
          :last_name => Faker::Name.last_name,
          :birthdate => randomDate(:year_range => 60, :year_latest => 22),
          :created_at => randomDate(:year_range => 4, :year_latest => 0),
          :city => Faker::Lorem.words(1).to_s.capitalize,
          :state => Faker::Address.us_state(),
          :country => @countries.rand.to_s,
          :password => "greatpasswordhuh",
          :password_confirmation => "greatpasswordhuh",
          :accepts_terms_and_conditions => true,
          :gender => @genders.rand.to_s,
          :email => Faker::Internet.email
        )

        u.extended_profile = ExtendedProfile.new(
          :bio => Faker::Lorem.sentences(5).join(" "),
          :occupation => Faker::Lorem.words(1).to_s.capitalize
        )

        u.preferences = Preferences.new()
        u.save!

      end
    end
  end
end

Note that while Faker was good for creating random names, e-mail addresses, etc., I needed some other data that was outside of its scope, for example gender and country. For these, I simply created an array (like @genders = ["Male","Female"]) and called @genders.rand to pick a value. I also used its Lorem Ipsum generator to create random city names, occupations, and biographical text.

Once the Rake task was written, it was simply a matter of typing rake db:development:fake_user_data to populate the database with my junk records. Pretty sweet!

You can find Faker at http://faker.rubyforge.org/.

Comments

cdmwebs
February 7, 2008 at 6:08 AM

Nice! I wrote one a lot like this <a href="http://26webs.com/blog/2008/02/02/a-rake-task-for-faker/" rel="nofollow">here</a>.
Leave a Comment


IdolHands.com Spam-o-MeterTM
Bot
Spammer
Moron
Human






* Required fields.