refactored code to reflect ActiveRecord dbase calls vs sql calls