Backport #9347 to rails 3.2