Fixed edge case SQL issue in rules module (#771)