convert functionality into activerecord