Rails3 backporting to Rails2

This is partial revisions of #258