Ensure HABTM relationships produce valid class names (Fixes #17119)

See https://github.com/rails/rails/issues/17119 for the back story.
Yeah, I cannot get the tests to run in JRuby. For me, it says `--dev` is not a valid option. On travis https://travis-ci.org/rails/rails/jobs/37502973#L223 it is not even loading to run the tests.