ActiveRecord的事务操作

begin
    Model.transaction do
        model.save!

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

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

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

你可能感兴趣的:(activerecord,ruby)