modified the logic to work with rails 4.1