3

以下是我的制造商:

Fabricator(:my_fabricator) do
  my_first_association
  my_second_association
end

这里的问题是我需要将 my_first_association 传递给 my_second_association。在文档中找不到任何相关内容。

4

1 回答 1

2

如果您传递一个块值,您可以检查正在生成的对象的属性哈希。

Fabricator(:my_fabricator) do
  my_first_association
  my_second_association do |attrs|
    Fabricate.build(:my_second_association, my_first_association: attrs[:my_first_association])
  end
end
于 2016-04-12T00:08:53.690 回答