小编rog*_*fed的帖子

保存事务失败后的rails对象

我有一个交易,以确保同时保存两个模型.

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)

当其中一个模型的验证失败时,它会进入救援区,但是在救援区中,它表示模型不是新记录.我期待验证导致事务失败,从而将模型对象留作新记录?我在这里失踪了什么?

validation transactions ruby-on-rails

3
推荐指数
1
解决办法
3514
查看次数

标签 统计

ruby-on-rails ×1

transactions ×1

validation ×1