Skip to content

Instantly share code, notes, and snippets.

@jwood jwood/questions.rake
Created Jul 13, 2015

Embed
What would you like to do?
Example of rake task that migrates production data
namespace :question do
desc "Update Questions and Tasks to support newly added validations"
task :update_for_validations => :environment do
ActiveRecord::Base.transaction do
# Set question_type to "yesno" for following questions:
%w(need_long_term_rental need_short_term_rental find_eye_doctor cleaning need_home).each do |identifier|
q = Question.find_by_identifier(identifier)
q.update_attributes!(question_type: "yesno") if q.present?
end
# Delete 6 tasks with name "Master Question - NO TASK"
master_question_tasks = Task.where(name: "Master Question - NO TASK")
master_question_tasks.each(&:destroy!)
end
puts "Verifying validity of remaining questions..."
all_questions_valid = true
Question.find_each do |q|
unless q.valid?
puts "Invalid question: [#{q.id}/#{q.identifier}] #{q.errors.full_messages}"
all_questions_valid = false
end
end
puts "All Questions are valid." if all_questions_valid
puts "Verifying validity of remaining tasks..."
all_tasks_valid = true
Task.find_each do |t|
unless t.valid?
puts "Invalid task: [#{t.id}/#{t.name}] #{t.errors.full_messages}"
all_tasks_valid = false
end
end
puts "All Tasks are valid" if all_tasks_valid
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.