There are several methods: first_or_create_by, find_or_create_byand so on, which operate on the principle:
- talk to the database to try and find the material we need.
- If we did not find it, do it yourself.
- save it to db
Obviously, simultaneous calls to these methods can have both threads that cannot find what they want, and in step 3 a crash will unexpectedly occur.
Seems like the best solution is
create_or_find
I.e:
- create reasonable uniqueness constraints in your database in advance.
- save something if you want to save it.
- if it worked, ok.
- if it does not work due to a RecordNotUnique exception, it is already there, fine, download it
, Rails, ( , ) create_or_find?