[V40] Rails 3.2 porting

rebased #750.