Pregunta

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?

¿Fue útil?

Solución

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top