refactor for rails