我有一个交易,以确保同时保存两个模型.
begin
Recipe.transaction do
@recipe.save!
if @dish
@dish.save!
end
end
rescue
#save failed
flash[:notice] = "recipe.saved = #{@recipe.new_record?}"
render 'form'
else
#save worked
flash[:notice] = 'Recipe added.'
redirect_to(@recipe)
end
Run Code Online (Sandbox Code Playgroud)
当其中一个模型的验证失败时,它会进入救援区,但是在救援区中,它表示模型不是新记录.我期待验证导致事务失败,从而将模型对象留作新记录?我在这里失踪了什么?