DEV Community

Discussion on: FactoryBot .find_or_create_by

Collapse
 
agrinko profile image
Alexey Grinko

I solved it a bit differently:

  instance.id = Country.where(code: instance.code).first_or_create(instance.attributes).id
Enter fullscreen mode Exit fullscreen mode

Not sure if it makes any difference with your sample above, but using instance.attributes makes it more generic as we don't have to explicitly mention each attribute.

Thread Thread
 
jooeycheng profile image
Joey Cheng

That’s neat! Yeah, this does seem simpler. Off the top of my head, I can’t think of any differences.