upsert votes to deduplicate
This commit is contained in:
parent
db6a2e6ff1
commit
0082f11d58
|
@ -28,19 +28,25 @@ class VotesController < ApplicationController
|
|||
|
||||
require 'csv'
|
||||
|
||||
CSV.parse(votes_csv, :headers => %i[voter_email short_name voter_login_id voter_password]) do |row|
|
||||
headers = %i[
|
||||
voter_email
|
||||
short_name
|
||||
voter_login_id
|
||||
voter_password
|
||||
]
|
||||
votes = CSV.parse(votes_csv, headers: headers).map { |row|
|
||||
csv_fields = row.to_hash
|
||||
email = csv_fields[:voter_email]
|
||||
user = User.find_by(room_id: room_id, email: email)
|
||||
vote_fields = {
|
||||
{
|
||||
:room_id => room_id,
|
||||
:user_id => user.id,
|
||||
:election_slug => csv_fields[:short_name],
|
||||
:voter_login_id => csv_fields[:voter_login_id],
|
||||
:voter_password => csv_fields[:voter_password],
|
||||
}
|
||||
Vote.create!(vote_fields)
|
||||
end
|
||||
}
|
||||
Vote.upsert_all(votes, unique_by: [:user_id, :election_slug])
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to room_users_url(room_id), notice: 'Votes were successfully created.' }
|
||||
|
|
Loading…
Reference in New Issue