假设我在 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
- 我在上面的代码中基本上做错了什么?(我意识到这可能在很多方面都很糟糕。)
- 从 Rails 的角度来看,这样做的正确方法是什么?
- 从数据库的角度来看,我应该注意哪些注意事项?(例如,即使像上面这样的代码是可能的,我猜它在数据库方面会过分。但我自然不太确定。)