Avoid duplicate records in poll answers
Until now, we've stored the text of the answer somebody replied to. The idea was to handle the scenarios where the user voters for an option but then that option is deleted and restored, or the texts of the options are accidentally edited and so the option "Yes" is now "Now" and vice versa. However, since commit3a6e99cb8, options can no longer be edited once the poll starts, so there's no risk of the option changing once somebody has voted. This means we can now store the ID of the option that has been voted. That'll also help us deal with a bug introduced int673ec075e, since answers in different locales are not counted as the same answer. Note we aren't dealing with this bug right now. We're still keeping (and storing) the answer as well. There are two reasons for that. First, we might add an "open answer" type of questions in the future and use this column for it. Second, we've still got logic depending on the answer, and we need to be careful when changing it because there are existing installations where the answer is present but the option_id is not. Note that we're using `dependent: nullify`. The reasoning is that, since we're storing both the option_id and the answer text, we can still use the answer text when removing the option. In practice, this won't matter much, though, since we've got a validation rule that makes it impossible to destroy options once the poll has started. Also note we're still allowing duplicate records when the option is nil. We need to do that until we've removed every duplicate record in the database.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
class AddQuestionAnswerIdToPollAnswers < ActiveRecord::Migration[7.0]
|
||||
def change
|
||||
change_table :poll_answers do |t|
|
||||
t.references :option, index: true, foreign_key: { to_table: :poll_question_answers }
|
||||
|
||||
t.index [:option_id, :author_id], unique: true
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[7.0].define(version: 2024_05_11_141119) do
|
||||
ActiveRecord::Schema[7.0].define(version: 2024_05_13_135357) do
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "pg_trgm"
|
||||
enable_extension "plpgsql"
|
||||
@@ -1024,7 +1024,10 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_11_141119) do
|
||||
t.string "answer"
|
||||
t.datetime "created_at", precision: nil
|
||||
t.datetime "updated_at", precision: nil
|
||||
t.bigint "option_id"
|
||||
t.index ["author_id"], name: "index_poll_answers_on_author_id"
|
||||
t.index ["option_id", "author_id"], name: "index_poll_answers_on_option_id_and_author_id", unique: true
|
||||
t.index ["option_id"], name: "index_poll_answers_on_option_id"
|
||||
t.index ["question_id", "answer"], name: "index_poll_answers_on_question_id_and_answer"
|
||||
t.index ["question_id"], name: "index_poll_answers_on_question_id"
|
||||
end
|
||||
@@ -1798,6 +1801,7 @@ ActiveRecord::Schema[7.0].define(version: 2024_05_11_141119) do
|
||||
add_foreign_key "moderators", "users"
|
||||
add_foreign_key "notifications", "users"
|
||||
add_foreign_key "organizations", "users"
|
||||
add_foreign_key "poll_answers", "poll_question_answers", column: "option_id"
|
||||
add_foreign_key "poll_answers", "poll_questions", column: "question_id"
|
||||
add_foreign_key "poll_booth_assignments", "polls"
|
||||
add_foreign_key "poll_officer_assignments", "poll_booth_assignments", column: "booth_assignment_id"
|
||||
|
||||
Reference in New Issue
Block a user