Adapted to activerecord 3.x