2011-03-20 33 views
0

有人可以提供一个例子或指向我在哪里我可以学习如何工厂女孩嵌套模型协会?使用工厂女孩创建线程和线程参与工厂

,线程必须有至少一个ThreadParticipation

现在,我有我的线程在factories.rb如下:

Factory.define :thread do |thread| 
    thread.title    "mythread" 
end 

如何再建立一个ThreadParticipation?

感谢

回答

3

Getting Started file在factory_girl源有关联的信息。

关联的实例可以通过使用当 限定lazy属性的关联方法来生成 :

factory :post do 
    # ... 
    author 
end 

也可以指定不同的 工厂或重写属性:

factory :post do 
    # ... 
    association :author, :factory => :user, :last_name => 'Writely' 
end 

因此,在你的例子中,我会想象这样的事情会做:

Factory.define :thread do |thread| 
    thread.title "mythread" 
    thread.thread_participation 
end 

Factory.define :thread_participation do |ppn| 
    ppn.attribute "value" 
end 

如果你使用一个集合,而不是一个has_one/belongs_to关联的,你可以创建一个数组这样:

Factory.define :thread do |thread| 
    thread.title "mythread" 
    thread.thread_participations { |a| [a.association(:thread_participation)] } 
end 
+0

感谢但散发出一种“未定义的局部变量或方法thread_participation” – AnApprentice 2011-03-20 03:37:15

+0

我正在使用导轨3工厂女孩宝石,也许这是为什么?你必须使用Factory.define:thread do | thread | – AnApprentice 2011-03-20 03:41:27

+1

啊,有道理。我编辑了答案。谢谢! – 2011-03-20 19:50:30