Adapted code for rails 4