Moved a sql query to models