ActiveRecord的事务操作

707 查看

begin
    Model.transaction do
        model.save!

        # 手工抛异常
        raise ActiveRecord::ActiveRecordError if ...
    end

rescue ActiveRecord::ActiveRecordError
    # 返回错误信息...
end

注意,捕获的异常是ActiveRecord::ActiveRecordError,而不是StandardError或者RuntimeError