Finally got the monster sql to run in rails.