Election results in the London Borough of Sutton.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

import-candidates-csv.rb 1.4 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. require_relative '../models'
  2. require 'csv'
  3. require 'pp'
  4. # $ ruby scripts/import-candidates-csv.rb [ELECTION ID] [CSV FILENAME]
  5. # Import a CSV file of election candidates for a specified election
  6. # The election for which you're importing data must already exist in the elections table
  7. @election = Election.get(ARGV.shift)
  8. pp @election
  9. candidates_matched = 0
  10. CSV.foreach(ARGV.shift) do |line|
  11. # pp line
  12. district_name, candidate_surname, candidate_forenames, party = line
  13. # District
  14. @district = District.first(:name => district_name)
  15. # pp @district
  16. # Candidate
  17. @candidate = Candidate.first_or_create(:forenames => candidate_forenames, :surname => candidate_surname)
  18. if @candidate
  19. pp @candidate
  20. candidates_matched += 1
  21. end
  22. unless @candidate.saved?
  23. $stderr.puts "Couldn't save candidate #{@candidate}"
  24. exit 1
  25. end
  26. # Party
  27. @party = Party.first_or_create(:name => party)
  28. puts @party.name
  29. unless @party.saved?
  30. $stderr.puts "Couldn't save party #{@party}"
  31. exit 1
  32. end
  33. # Candidacy
  34. @candidacy = Candidacy.create(
  35. :election => @election,
  36. :candidate => @candidate,
  37. :party => @party,
  38. :district => @district,
  39. :votes => nil
  40. )
  41. unless @candidacy.saved?
  42. $stderr.puts "Couldn't save candidacy #{@candidacy}"
  43. exit 1
  44. end
  45. end
  46. puts "Candidates: #{Candidate.count}"
  47. puts "Candidacies: #{Candidacy.count}"
  48. puts candidates_matched