我有以下代码:
rating = user.recipe_ratings.where(:recipe_id => recipe.id).where(:delivery_id => delivery.id).first_or_create
然而不知何故,我们偶尔会PG::Error: ERROR: duplicate key value violates unique constraint
从中得到错误。我想不出任何应该发生的原因,因为重点first_or_create
是防止这些。
这只是一个疯狂的比赛条件吗?begin...rescue
如果没有一系列令人抓狂的障碍,我该如何解决这个问题?