2011-11-08 23 views
0
invitation ------> event 
    \      \ 
    \/     \/ 
responder(person) ---->account 
     \     /\ 
     \    /
     group------------- 

我有上述關聯圖。 及以下的工廠代碼:工廠女孩共同根目錄圖

Factory.define :invitation do |i| 
    i.association :event 
    i.association :responder, :factory => :person 
end 


Factory.define :event do |e| 
    e.association :account 
end 


Factory.define :person do |p| 
    p.association :account 
    p.association :group 
end 

Factory.define :group do |g| 
    g.association :account 
end 

如果我想創建一個邀請「工廠:邀請函」,那麼該帳戶將被創建不止一次。第二次嘗試已經採用了獨特的賬戶領域。 有沒有什麼聰明又好的收據?

回答

1

您可以重複使用after_create(或after_build)回調中的事件帳戶。

Factory.define :invitation do |i| 
    i.association :event 
    i.after_create { |i| i.responder = Factory(:person, :account => i.event.account) } 
end 

您必須以類似的方式修改組工廠。

+0

hm ...讓我檢查一下:) – Boti

+0

使用after_build後,應該會更好。 – mb14