DEV Community

Cover image for FactoryBot .find_or_create_by

FactoryBot .find_or_create_by

Joey Cheng on March 18, 2019

TL;DR factory :country do to_create do |instance| instance.id = Country.find_or_create_by(name: instance.name, code: instance.co...
Collapse
 
shamox profile image
Roland Laurès

Hi !
Thanks for the article, nevertheless, it is working great when you don't have any other attributes in your object that needs to be validated.

I made a little git project to illustrate : github.com/ShamoX/country_test

commit: fd40c203 show the introduction of the problem.

Adding the new attribute here in the find_or_create_by doesn't work because we want inhabitants number to be random.

My solution then consist to first have to look for an existing entry only on the unique field, and then return the old record with it's own value...

What do you think ?

Collapse
 
jooeycheng profile image
Joey Cheng • Edited

Hey Roland!

Firstly, apologies for the late reply. 🙏🏻
I'm happy that the article helped you!

If I understand your requirements correctly - you want to be able to find_by "country code", but create with random "inhabitants". Something lesser known in Rails - there is a method exactly just for that:

FactoryBot.define do
  factory :country do
    name { 'Canada' }
    code { 'CA' }
    inhabitants { SecureRandom.rand(10..100_000_000) }

    to_create do |instance|
      instance.id = Country.create_with(name: instance.name, inhabitants: instance.inhabitants)
                           .find_or_create_by!(code: instance.code)
                           .id
      instance.reload
    end
  end
end

This would find_by "code", and return if exists. Else, it will create with "code", along with "name" and "inhabitants".

I wrote that off the top of my mind. Can you test to make sure it works?

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.

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.

Collapse
 
edisonywh profile image
Edison Yap

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

Collapse
 
jooeycheng profile image
Joey Cheng

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.

Collapse
 
fatkhanfauzi profile image
FatkhanFauzi

nice !!
it works !!