diff --git a/app/models/candidate.rb b/app/models/candidate.rb index 3ceda3a..e2ac92b 100644 --- a/app/models/candidate.rb +++ b/app/models/candidate.rb @@ -5,12 +5,25 @@ class Candidate belongs_to :constituency belongs_to :party + has_many :votes alias_method :to_s, :name def name_and_party "%s (%s)" % [name, party.name] end + + def share_of_vote + if constituency.vote_count > 0 + "%0.1f%%" % (100 * votes.count / constituency.vote_count.to_f) + else + '-' + end + end + + def vote_count + @vote_count ||= votes.count + end end