I have the following code in a rail model:
foo = Food.find(...) foo.with_lock do if bar = foo.bars.find_by_stuff(stuff) # do something with bar else bar = foo.bars.create! # do something with bar end end
The goal is to make sure that the Bar of the created type is not created twice.
Testing with_lock works on the console, confirms my expectations. However, in production, apparently, in any case or in all cases, the lock does not work as expected, and a backup bar is created - so using the with_lock function (always?), A code arises waiting to be rotated.
What could be here?
The update is so sorry to everyone who said that โblocking foo will not help youโ !! My example did not initially get a search in the bar. this is now fixed.
John bachir
source share