FactoryBot .find_or_create_by

Joey Cheng on March 18, 2019

There seems to be no clear documentation on how to go about this. So, here's what I tried. Before we begin, let's prepare our test environment, ... [Read Full]
markdown guide
 

Great article! Didn't know you could do this on FactoryBot. Is this similar to the FactoryBot's use_parent_strategy?

 

use_parent_strategy is something different (more info on their docs), it tells FactoryBot whether or not to use the parent's strategy (eg: build or create).

For example, given a model User and Country (User belongs_to Country), when use_parent_strategy=true, calling build(:user) will also build (instead of create) the associated Country, because it follows the "parent strategy of build".

However, FactoryBot custom strategies is something different that I want to explore. Perhaps defining a new find_or_create strategy.

code of conduct - report abuse