Say I want something like this in Rails:
class Proposal < ActiveRecord::Base
def interest_level
self.yes_votes.count - self.no_votes.count
end
private
def yes_votes
self.votes.where(:vote => true)
end
def no_votes
self.votes.where(:vote => false)
end
end
- What have I basically done wrong in the code above? (I realize it's probably terrible in numerous ways.)
- What's the correct way to do this from a Rails standpoint?
- What considerations should I be mindful of from a database standpoint? (e.g., even if code like the above were possible, I'm guessing it would be excessive on the DB side. But naturally I'm not really sure.)