我有3个模特
class Account
end
class AccountType
end
class Lookup
end
我想创建一个工厂
1 factory :account_test, class: 'Account' do
2 sequence(:name) {|i| "Account #{i}"}
3 association :account_type, factory: :account_type_with_data
4 association :lookup, factory: :lookup_value_with_account_type
5 active "1"
6 end
7
8 factory :lookup_value_with_account_type, class: 'Lookup' do
9 association :account_type, ????????
10 end
如果是第二个工厂,我想使用工厂第3行中生成的account_type的值为第9行。我该怎么办
答案 0 :(得分:1)
我想您要查看transient
块和after
回调。
我在这个工厂里做了类似的事情,在那里我获取了最近创建的event_session
的属性,并使用它们来创建和事件会话发布。
希望这有助于或者指出正确的方向。
# Usage Notes:
# Use create(:event_session, create_post: true) to create associated event session post using event_session's title and body.
FactoryGirl.define do
factory :event_session do
transient do
event_session nil
create_post false
end
name {Faker::Commerce.product_name}
description { Faker::Company.catch_phrase + '. ' + Faker::Company.catch_phrase }
start_date_time { Time.now }
end_date_time { Time.now + 1.hours }
track_name { Faker::Company.catch_phrase }
session_type { Faker::Commerce.product_name }
room_name { Faker::Commerce.product_name }
sponsor { create(:sponsor) }
display_rank [*1..100].sample
event { create(:event, :random)}
sequence(:client_id) { |n| "client_id_#{n}" }
after(:create) do |event_session, evaluator|
create(:post, event_session: event_session, title: evaluator.name, body: evaluator.description) if @overrides.present? && @overrides[:create_post] || evaluator == true
end
end
end