すべての依存関係 (特にポリモーフィズム) を説明するモデルに十分な数のレコードを作成する単純なスクリプトをデータベースに入力することで、テスト データの生成を最初に試みました。
これは私の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
例として、question
工場を次に示します。
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
Random
クラスは1 つのランダムな用語を生成しますが、その用語は作成されたすべてのインスタンスで同じままです。この場合、「スペインで旅行するのに最適な場所はどこですか?」などの 50 の質問が表示されます。そして、それぞれに同一の 2 つのテキスト パラグラフがあります。
私は何が欠けていますか?