code from railscast #417