#First assign a user to user variable user = User.find_by(username: 'your username here') Find all failed jobs for the project with the id 62 job = Ci::Build.find_by_sql("select * from ci_builds where status = 'failed' and project_id = '62'") job.each { |j| Ci::Build.retry(j, user) }