5

I gave generating test data a first shot by trying to populate my database with a simple script that creates a sufficient number of records for my models accounting for all dependencies (esp. polymorphism).

This is my seeds.rb

require 'factory_girl_rails'

50.times do

  @user = FactoryGirl.create(:user)
  FactoryGirl.create(:contact, :user => @user)

  @question = FactoryGirl.create(:question, :user => @user)

  FactoryGirl.create(:user_answer, :question => @question, :authorable => @user)

  @contact = FactoryGirl.create(:contact, :user => @user)
  FactoryGirl.create(:contact_answer, :question => @question, :authorable => @contact)

end

As an example, here ist the question factory:

FactoryGirl.define do
  factory :question do
    title       "What is the best place to travel in " + Random.country + "?"
    body        Random.paragraphs(2)
    association :user, :method => :build
  end
end

While the Random class does produce one random term, that term remains the same for all instances created. In this case I would get 50 questions of, say, "What is the best place to travel in Spain?" and the identical two paragraphs of text for each.

What am I missing?

Mureinik
  • 297,002
  • 52
  • 306
  • 350
matt_jay
  • 1,241
  • 1
  • 15
  • 33
  • `seeds.rb` should only be used to add required 'static' data to the database, not to 'fixture' it. To create base data so you can manually test the application on development, you should create a rake task (we have `rake db:populate`) – fuzzyalej Feb 07 '12 at 08:50
  • Thanks for pointing that out. Would you expect the behavior to change, though, if I were to run the same in a separate task? – matt_jay Feb 07 '12 at 10:11

1 Answers1

3

So I'm not sure where the Random class is coming from here. But I always used the Faker gem for this stuff.

It does names, emails, cities, phone numbers: like this:

Faker::Name.name
Faker::Address.uk_country
Faker::Lorem.paragraph

check it out

Matthew
  • 12,892
  • 6
  • 42
  • 45
  • FYI @Matthew: [This is where the Random class comes from](https://github.com/tomharris/random_data). – matt_jay Feb 21 '12 at 21:53