开发者

Error Handling in ActiveRecord Transactions?

I need to create a row in both tickets and users table... I just need to know how to process in case the transaction fails.

@ticket.transaction do
    @ticket.save!
    @user.save!
end
    #if (transaction succeeded)
        #.....
    #else (transaction failed)
        #......
    #e开发者_C百科nd

On a side note I'd just like to thank everyone who participates at stack overflow for helping a designer learn more programming... I appreciate the time you guys take out of your day to answer n00b questions like this :)


If you are using the save! method with a bang (exclamation point), the application will throw an exception when the save fails. You would then have to catch the exception to handle the failure.

begin
  @ticket.transaction do
    @ticket.save!
    @user.save!
  end
  #handle success here
rescue ActiveRecord::RecordInvalid => invalid
   #handle failure here
end


i'm also a beginner, but i believe that you can check @ticket.errors and @user.errors and validate according to their responses

also the save method should return a boolean that determines if the save was successful

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜