diff --git a/app/controllers/attempts_controller.rb b/app/controllers/attempts_controller.rb index ec571f83..0d683cb4 100644 --- a/app/controllers/attempts_controller.rb +++ b/app/controllers/attempts_controller.rb @@ -4,7 +4,7 @@ class AttemptsController < ApplicationController def twilio recipient = Recipient.where(phone: twilio_params['From']).first - attempt = recipient.attempts.last + attempt = recipient.attempts.last_sent.first if (twilio_params[:Body].downcase == 'stop') attempt.recipient.update_attributes(opted_out: true) diff --git a/app/models/attempt.rb b/app/models/attempt.rb index c96cfc50..527f7641 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -19,6 +19,7 @@ class Attempt < ApplicationRecord scope :with_answer, -> { where('answer_index is not null or open_response_id is not null')} scope :with_no_answer, -> { where('answer_index is null and open_response_id is null')} scope :not_yet_responded, -> { where(responded_at: nil) } + scope :last_sent, -> { order(sent_at: :desc) } def messages if student.present? diff --git a/spec/controllers/attempts_controller_spec.rb b/spec/controllers/attempts_controller_spec.rb index 5c9cf2a8..ec96ad2b 100644 --- a/spec/controllers/attempts_controller_spec.rb +++ b/spec/controllers/attempts_controller_spec.rb @@ -26,7 +26,8 @@ RSpec.describe AttemptsController, type: :controller do schedule: schedule, recipient: recipients.first, recipient_schedule: recipient_schedule, - question: questions.first + question: questions.first, + sent_at: Time.new ) } let!(:attempt) { @@ -34,7 +35,8 @@ RSpec.describe AttemptsController, type: :controller do schedule: schedule, recipient: recipients.first, recipient_schedule: recipient_schedule, - question: questions.first + question: questions.first, + sent_at: Time.new ) } let!(:attempt2) { @@ -42,7 +44,8 @@ RSpec.describe AttemptsController, type: :controller do schedule: schedule, recipient: recipients.last, recipient_schedule: recipient_schedule2, - question: questions.first + question: questions.first, + sent_at: Time.new ) }