[V2] Rails3 backporting to Rails2

This is partial revisions of #273.
Revised #259.