Refactoring for rails 4 and fix bug