如何挽救模型交易并向用户显示错误?

因此,假设您有2个模型,人员和地址,每人只有一个地址可以标记为“主要”。 因此,如果我想更改一个人的主要地址,我需要使用一个事务,将新的标记标记为主要标记并取消标记旧标记。 据我所知在控制器中使用事务并不好,所以我在模型中有一个特殊的方法,这就是我所拥有的:

AddressesController < ApplicationController def update @new_address = Address.find(params[:id]) @old_address = Address.find(params[:id2]) @new_address.exchange_status_with(@old_address) end end 

模型:

 class Address < ActiveRecord::Base def exchange_status_with(address) ActiveRecord::Base.transaction do self.save! address.save! end end end 

所以问题是,如果模型方法中的事务失败,我需要解救它并通知用户有关错误的信息,我该怎么做? 有没有办法让这个模型方法返回true或false,具体取决于事务是否成功,就像save方法一样?

我可能可以将该事务放在控制器中并在救援部分中呈现错误消息,但我猜它不对或者我可以将该方法放入回调中,但想象有一些原因我不能这样做,是什么替代方案?

PS不要注意查找带有params id和id2的实例,只是随机的事情来表明我有2个实例

 def exchange_status_with(address) ActiveRecord::Base.transaction do self.save! address.save! end rescue ActiveRecord::RecordInvalid => exception # do something with exception here end 

仅供参考,例外情况如下:

 # 

和:

 exception.message # => "Validation failed: Email can't be blank" 

旁注,你可以改变self.save! save!


如果您想保持活动模型错误,请替代解决方案:

 class MyCustomErrorClass < StandardError; end def exchange_status_with(address) ActiveRecord::Base.transaction do raise MyCustomErrorClass unless self.save raise MyCustomErrorClass unless address.save end rescue MyCustomErrorClass # here you have to check self.errors OR address.errors end