+ # callbacks
+ after_update :save_rankings
+ before_destroy :destroy_rankings
+
+
+
+ def to_s
+ votes.join("")
+ end
+
+ def each
+ self.votes.each {|vote| yield vote}
+ end
+
+ def votes
+ unless @votes
+ if rankings.empty?
+ @votes = Array.new
+ else
+ @votes = rankings.sort.collect { |ranking| ranking.candidate.id }
+ end
+ end
+
+ @votes
+ end
+
+ def votes=(array)
+ @votes = array
+ end
+
+ def save_rankings
+ destroy_rankings
+ self.votes.each_with_index do |candidate_id, index|
+ ranking = Ranking.new
+ ranking.rank = index
+ ranking.candidate = Candidate.find(candidate_id)
+ self.rankings << ranking
+ end
+ end
+
+ def destroy
+ self.destroy_rankings