Guest User

Untitled

a guest
May 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. I have many Users that Vote on Requirements
  2.  
  3. Each user can vote on a requirement on more than one criteria.
  4.  
  5. So I want to get a list of all of the votes in a criteria, in a two-d array like such. There are some places where there is no vote. In that case the value in the array is an empty string
  6.  
  7. User 1 User 2 User 3
  8. Req 1 9 4 5
  9. Req 2 1 "" ""
  10. Req 3 "" 4 0
  11.  
  12.  
  13. I currently get the array using this method but it is very slow as there are thousands of votes.
  14.  
  15.  
  16. class Criteria < ActiveRecord::Base
  17. set_table_name "criteria"
  18. set_primary_key :criteria_id
  19. belongs_to :instance
  20. has_many :votes
  21.  
  22. def build_vote_array
  23. vote_array = Array.new(instance.requirements.count){|req_i|
  24. Array.new(instance.project.stakeholders.count) {|stake_i|
  25. vote = self.votes.select{|v|
  26. v.requirement_id == instance.requirements[req_i].id &&
  27. v.user_id == instance.project.stakeholders[stake_i].id
  28. }[0]
  29. vote == nil ? "" : vote.value
  30. }
  31. }
  32.  
  33. end
  34. end
Add Comment
Please, Sign In to add comment