How to randomize data using seed.rb using Factory Girl and random_data?

I gave the first shot the test data generation, trying to populate my database with a simple script that creates enough records for my models to take into account all the dependencies (especially 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 is a questionfactory:

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 class Randomcreates one random term, this term remains unchanged for all instantiated instances. In this case, I would get 50 questions, say, "What is the best place to travel to Spain?" and identical two paragraphs of text for each.

What am I missing?

+5
source share
1 answer

, , Random. Faker gem .

, , , : :

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

+3

All Articles