Skip to content

fix: Tries to fix the survey behavior - #1015

Open
AyakorK wants to merge 2 commits into
developfrom
fix/surveys_behavior
Open

fix: Tries to fix the survey behavior #1015
AyakorK wants to merge 2 commits into
developfrom
fix/surveys_behavior

Conversation

@AyakorK

@AyakorK AyakorK commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

🎩 Description

Fixes duplicate answers on questionnaires that allow unregistered (non-logged-in) respondents.

The root cause was in session_token, used to identify anonymous respondents: it read session[:session_id], a key that is never actually assigned anywhere in the codebase. This made session_token return the same blank value for every anonymous visitor, so different respondents became indistinguishable from one another once answers were stored/exported, which is what looked like duplicates. It now uses request.session.id, the actual Rails session identifier, giving each anonymous visitor a stable, unique token.

On top of that:

  • AnswerQuestionnaire now uses find_or_initialize_by instead of Answer.new, so a resubmission for the same respondent/question updates the existing answer instead of creating a second one.
  • A uniqueness validation was added on Answer as a model-level safety net against duplicates, independent of the two fixes above.
  • UserAnswersSerializer now captures question positions once at the start of an export instead of re-reading them live per answer, to avoid inconsistent column mapping if the questionnaire structure changes mid-export.

None of this changes the respondent-facing experience, same forms, same flow, just correct identification and data underneath.

There have also been separate, unconfirmed reports of answers appearing shifted/misaligned on submit and/or export. We could not pin down a cause for that with certainty. It's possible (not confirmed) that the session_token fix also alleviates part of it, since before this fix, every unregistered respondent shared the same blank identifier, meaning several different people's answers could end up merged under a single respondent row in an export, which could look like misalignment. This is a plausible side-effect at best, not a verified fix for that specific report.

📌 Related Issues

Tasks

  • surveys_controller_extends.rb: fixed session_token/visitor_already_answered? to use request.session.id instead of the never-assigned session[:session_id]
  • answer_questionnaire_extends.rb: switched to find_or_initialize_by instead of Answer.new to make answer submission idempotent
  • answer_extends.rb: added a uniqueness validation on Answer as a model-level duplicate guard
  • user_answers_serializer_extends.rb: cache question positions once at the start of export instead of re-reading them live per answer
  • Add specs

@AyakorK
AyakorK marked this pull request as ready for review August 10, 2026 07:38
@AyakorK
AyakorK requested a review from moustachu August 10, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant