fix for #10613 . This PR is still WIP.

I'm sending this PR in response to this comment. https://github.com/rails/rails/issues/10613#issuecomment-26210557

I'm not sure if this is the best fix. And it does not have any test yet.

I guess PR will get more scrutiny and comments compared to a branch lying in my repo.