简体   繁体   中英

Fabrication gem associations that depend on each other

The following is my fabricator:

Fabricator(:my_fabricator) do
  my_first_association
  my_second_association
end

The problem here is that I need to pass my_first_association to my_second_association. Couldn't find anything related in the docs .

If you pass a block value you can inspect the attributes hash of the object being generated.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM