cherry picked commit 8e49340cd577b3fc0b582025c8843f30a00de269 from rails32 branch