cherry picking https://github.com/makandra/rails/commit/c3b338b67d6cf3d997e1e44070de6def4c6cda7a