You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sqm-dashboards/app/models/recipient_schedule.rb

30 lines
739 B

class RecipientSchedule < ApplicationRecord
belongs_to :recipient
belongs_to :schedule
has_many :attempts
def next_question
upcoming = upcoming_question_ids.split(/,/)
Question.where(id: upcoming.first).first
end
def make_attempt(question: next_question)
sent_at = Time.new
recipient.attempts.create(
schedule: schedule,
recipient_schedule: self,
question: question,
sent_at: sent_at
)
upcoming = upcoming_question_ids.split(/,/)[1..-1].join(',')
attempted = (attempted_question_ids.split(/,/) + [question.id]).join(',')
update_attributes(
upcoming_question_ids: upcoming,
attempted_question_ids: attempted,
last_attempt_at: sent_at
)
end
end