Fix rails console --sandbox

I've also added a proper acceptance test which reproduced the issue.

Closes #9513, #9515.