Add option_id to partial results and unique index

Similar to what we did in PR "Avoid duplicate records in poll answers" 5539,
specifically in commit 503369166, we want to stop relying on the plain text
"answer" and start using "option_id" to avoid issues with counts across
translations and to add consistency to the poll_partial_results table.

Note that we also moved the `possible_answers` method from Poll::Question to
Poll::Question::Option, since the list of valid answers really comes from the
options of a question and not from the question itself. Tests were updated
to validate answers against the translations of the assigned option.

Additionally, we renamed lambda parameters in validations to improve clarity.
This commit is contained in:
taitus
2025-09-10 12:22:08 +02:00
parent f2153f2b4d
commit a29eeaf2e2
13 changed files with 137 additions and 51 deletions

View File

@@ -0,0 +1,9 @@
class AddOptionIdToPollPartialResults < ActiveRecord::Migration[7.1]
def change
change_table :poll_partial_results do |t|
t.references :option, index: true, foreign_key: { to_table: :poll_question_answers }
t.index [:booth_assignment_id, :date, :option_id], unique: true
end
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2025_08_14_122241) do
ActiveRecord::Schema[7.1].define(version: 2025_09_09_145207) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "plpgsql"
@@ -1084,9 +1084,12 @@ ActiveRecord::Schema[7.1].define(version: 2025_08_14_122241) do
t.text "amount_log", default: ""
t.text "officer_assignment_id_log", default: ""
t.text "author_id_log", default: ""
t.bigint "option_id"
t.index ["answer"], name: "index_poll_partial_results_on_answer"
t.index ["author_id"], name: "index_poll_partial_results_on_author_id"
t.index ["booth_assignment_id", "date", "option_id"], name: "idx_on_booth_assignment_id_date_option_id_2ffcf6ea3b", unique: true
t.index ["booth_assignment_id", "date"], name: "index_poll_partial_results_on_booth_assignment_id_and_date"
t.index ["option_id"], name: "index_poll_partial_results_on_option_id"
t.index ["origin"], name: "index_poll_partial_results_on_origin"
t.index ["question_id"], name: "index_poll_partial_results_on_question_id"
end
@@ -1799,6 +1802,7 @@ ActiveRecord::Schema[7.1].define(version: 2025_08_14_122241) do
add_foreign_key "poll_officer_assignments", "poll_booth_assignments", column: "booth_assignment_id"
add_foreign_key "poll_partial_results", "poll_booth_assignments", column: "booth_assignment_id"
add_foreign_key "poll_partial_results", "poll_officer_assignments", column: "officer_assignment_id"
add_foreign_key "poll_partial_results", "poll_question_answers", column: "option_id"
add_foreign_key "poll_partial_results", "poll_questions", column: "question_id"
add_foreign_key "poll_partial_results", "users", column: "author_id"
add_foreign_key "poll_question_answer_videos", "poll_question_answers", column: "answer_id"