changed sql statement to active record methods to fix sqlite vs postgres differences