DEV Community

Discussion on: FactoryBot .find_or_create_by

Collapse
 
stuartspencer profile image
stuartspencer

I went with a variation:

to_create do |instance|
  instance.id =
    Country.
      find_or_create_by(
        name: instance.name,
        code: instance.code
      ).id
  instance.reload
end
Collapse
 
jooeycheng profile image
Joey Cheng

Nice, I assume it still works because the model will reload based on the assigned ID. This approach might be more performant than mine, because I attempt to update all attributes.