TL;DR
factory :country do
to_create do |instance|
instance.id = Country.find_or_create_by(name: instance.name, code: instance.co...
For further actions, you may consider blocking this person and/or reporting abuse
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 ?
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:
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?
I solved it a bit differently:
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.That’s neat! Yeah, this does seem simpler. Off the top of my head, I can’t think of any differences.
I went with a variation:
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.
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 ofbuild
".However, FactoryBot custom strategies is something different that I want to explore. Perhaps defining a new
find_or_create
strategy.nice !!
it works !!