在ActiveRecord模型中,after_save回调我需要ROLLBACK事务并返回false.
def after_save_callback if mycondition? raise ActiveRecord::Rollback end end
这个回调回滚事务但mymodel.save!返回true.如何让它返回false和rollback?
解决方法
我不认为你可以用after_save做到这一点你应该看看around_save:
def around_save ActiveRecord::Base.transaction do yield # calls the actual save method raise ActiveRecord::Rollback if my_condition? end end