Skip to content

Instantly share code, notes, and snippets.

@eamonn-webster
Last active December 28, 2015 05:39
Show Gist options
  • Save eamonn-webster/7451410 to your computer and use it in GitHub Desktop.
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
# 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
@bpaul
Copy link

bpaul commented Nov 13, 2013

What about using batches and transactions to speed it up? Start a transaction, do a big batch, commit transaction...

@eamonn-webster
Copy link
Author

It would speed up, but do we want it fast? If we were to run it in the background longer transactions more interference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment