Note that Rails 7.1 changes `find_or_create_by!` so it calls `create_or_find_by!` when no record is found, meaning we'll rarely get `RecordNotUnique` exceptions when using this method during a race condition. Adding this index means we need to remove the uniqueness validation. According to the `create_or_find_by` documentation [1]: > Columns with unique database constraints should not have uniqueness > validations defined, otherwise create will fail due to validation > errors and find_by will never be called. We're adding a test that checks what happens when using `create_or_find_by!`. Note that we're creating voters combining `create_with` with `find_or_create_by!`. Using `find_or_create_by!(...)` with all attributes (including non-key ones like `origin`) fails when a voter already exists with different values, e.g. an existing `origin: "web"` and an incoming `"booth"`. In this situation the existing record is not matched and the unique index raises an exception. `create_with(...).find_or_create_by!(user: ..., poll: ...)` searches by the unique key only and applies the extra attributes only on creation. Existing voters are returned unchanged, which is the intended behavior. For the `take_votes_from` method, we're handling a (highly unlikely, but theoretically possible) scenario where a user votes at the same time as taking voters from another user. For that, we're doing something similar to what `create_or_find_by!` does: we're updating the `user_id` column inside a new transaction (using a new transactions avoids a `PG::InFailedSqlTransaction` exception when there are duplicate records), and deleting the existing voter when we get a `RecordNotUnique` exception. On `Poll::WebVote` we're simply raising an exception when there's already a user who's voted via booth, because the `Poll::WebVote#update` method should never be called in this case. We still need to use `with_lock` in `Poll::WebVote`, but not due to duplicate voters (`find_or_create_by!` method will now handle the unique record scenario, even in the case of simultaneous transactions), but because we use a uniqueness validation in `Poll::Answer`; this validation would cause an error in simultaneous transactions. [1] https://api.rubyonrails.org/v7.1/classes/ActiveRecord/Relation.html#method-i-create_or_find_by
52 lines
1.5 KiB
Ruby
52 lines
1.5 KiB
Ruby
class Poll
|
|
class Voter < ApplicationRecord
|
|
VALID_ORIGINS = %w[web booth letter].freeze
|
|
|
|
belongs_to :poll
|
|
belongs_to :user
|
|
belongs_to :geozone
|
|
belongs_to :booth_assignment
|
|
belongs_to :officer_assignment
|
|
belongs_to :officer
|
|
|
|
validates :poll_id, presence: true
|
|
validates :user_id, presence: true
|
|
validates :booth_assignment_id, presence: true, if: ->(voter) { voter.origin == "booth" }
|
|
validates :officer_assignment_id, presence: true, if: ->(voter) { voter.origin == "booth" }
|
|
|
|
validates :document_number, presence: true, unless: :skip_user_verification?
|
|
validates :origin, inclusion: { in: ->(*) { VALID_ORIGINS }}
|
|
|
|
before_validation :set_demographic_info, :set_document_info, :set_denormalized_booth_assignment_id
|
|
|
|
scope :web, -> { where(origin: "web") }
|
|
scope :booth, -> { where(origin: "booth") }
|
|
scope :letter, -> { where(origin: "letter") }
|
|
|
|
def set_demographic_info
|
|
return if user.blank?
|
|
|
|
self.gender = user.gender
|
|
self.age = user.age
|
|
self.geozone = user.geozone
|
|
end
|
|
|
|
def set_document_info
|
|
return if user.blank?
|
|
|
|
self.document_type = user.document_type
|
|
self.document_number = user.document_number
|
|
end
|
|
|
|
def skip_user_verification?
|
|
Setting["feature.user.skip_verification"].present?
|
|
end
|
|
|
|
private
|
|
|
|
def set_denormalized_booth_assignment_id
|
|
self.booth_assignment_id ||= officer_assignment&.booth_assignment_id
|
|
end
|
|
end
|
|
end
|