Last active
December 28, 2015 05:39
-
-
Save eamonn-webster/7451410 to your computer and use it in GitHub Desktop.
set record_questions attempt
deal with those question_records that have a schedule_id instead of an enrollment_id
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# could get a speed up if we initialized to 1 instead of 0 | |
# but then we would have to compute all, no way to lazy compute. | |
# where ar we using the value? | |
def set_question_records_attempt_2(enterprise_id) | |
previous_enrollment_id = nil | |
previous_question_id = nil | |
attempt = 0 | |
QuestionRecord.where(enterprise_id: enterprise_id).where('enrollment_id is not null').order(:enrollment_id, :question_id, :created_at).each do |qr| | |
next if qr.enrollment_id.nil? | |
if qr.enrollment_id != previous_enrollment_id || qr.question_id != previous_question_id | |
attempt = 0 | |
end | |
attempt += 1 | |
if qr.attempt != attempt | |
QuestionRecord.where(id: qr.id).update_all(attempt: attempt) | |
end | |
previous_enrollment_id = qr.enrollment_id | |
previous_question_id = qr.question_id | |
end | |
previous_enrollment_id = nil | |
previous_question_id = nil | |
attempt = 0 | |
QuestionRecord.where(enterprise_id: enterprise_id).where('enrollment_id is null').order(:schedule_id, :question_id, :created_at).each do |qr| | |
next if qr.schedule_id.nil? | |
if qr.schedule_id != previous_enrollment_id || qr.question_id != previous_question_id | |
attempt = 0 | |
end | |
attempt += 1 | |
if qr.attempt != attempt | |
QuestionRecord.where(id: qr.id).update_all(attempt: attempt) | |
end | |
previous_enrollment_id = qr.schedule_id | |
previous_question_id = qr.question_id | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would speed up, but do we want it fast? If we were to run it in the background longer transactions more interference.