2016-05-01 7 views
0

に関連したレコードを作成しますI持っているユーザを作成するために、次の工場の女の子:あなたは、ユーザーの役割を定義するために、私はtrait Sを使っ見ることができるようにRailsのファクトリー・ガール:ユーザーではなく役割

FactoryGirl.define do 
factory :user do 
    first_name 'Colin' 
    last_name 'Ambler' 
    password 'dell13a1' 
    address_zip '60657' 

    trait :event_operator do 
    role Role.find_or_create_by(role_title: "Event Operator", name: "event_operator") 
    email "[email protected]" 
    end 

    trait :athletic_trainer do 
    role Role.find_or_create_by(role_title: "Athletic Trainer", name: "athletic_trainer") 
    email "[email protected]" 
    end 

end 
end 

と別のメールアドレスを設定してください。

私はEventと呼ばれる別のモデル、イベントは協会の名前だathletic_trainerに属し、それだけでUserモデルを指すので、私はこのようなイベントを作成するためのファクトリー・ガールを使用してい:

FactoryGirl.define do 

    factory :event do 
     event_type Event.event_types[:tournament] 
     sport Event.sports[:lacrosse] 
     event_title "NXT Cup" 
     event_logo { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'nxt_cup_logo.png')) } 
     gender Event.genders[:boys] 
     event_pay_rate 40 
     event_participant_numbers 3 
     event_feature_photo { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'support', 'images', 'nxt_cup_feature.jpg')) } 
     event_description "We roll out the red carpet for North America's top youth talent. The nations top youth teams descend on the premier facilities in the Philadelphia region to battle it out and stake claim to one of the summers most highly coveted trophies...the NXT Cup." 
     event_operator 
    end 

    end 

あなたが見ることができます私はユーザーの関連付けを指定するためのイベントの工場の女の子の末尾にevent_operatorを追加しますが、私は工場の女の子に関連付けられているユーザーにアクセスできません。

athletic_trainerをイベントの工場の女の子にパラメータとして送信することは可能ですか?私は工場の女の子を使ってユーザーを作成してイベントを作成したいが、今作成したユーザーを関連付けることが可能なのだろうか?

答えて

1

フィールドは、buildまたはcreateメソッドに渡すことで、いつでもフィールドをオーバーライドできます。 event_operatorを希望するuserに設定するには、次のように設定します。

user = build(:user) 
event = build(:event, event_operator: user) 
関連する問題