Rails code refactor