refactor the code for last assignment, applied active record virtual attribute and memorization techniques