Convert sqls to active record