Rail object in salvation after transaction failure is saved

I have a transaction that saves two models at the same time.

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

when the check is not performed for one of the models that it sends to the rescue unit, however, the disaster recovery unit says that the model is not a new record. Did I expect the validation to fail the transaction, leaving the model object as a new record? What am I missing here?

+3
source share
3 answers

Which of the two saves really doesn't work? One for @recipeor for @dish?

. , @dish , @recipe , . Rails @recipe.

+5

, , , , , .

Perhaps you are not using InnoDB tables? MyISAM does not support transactions.

0
source

Source: https://habr.com/ru/post/1733737/


All Articles