Convert sqls to active record (2)