【发布时间】:2010-09-23 07:47:08
【问题描述】:
假设我想要在 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 的角度来看,正确的方法是什么?
- 从数据库的角度来看,我应该注意哪些注意事项? (例如,即使上面的代码 是可能的,我猜它在数据库方面也会过分。但我自然不太确定。)
【问题讨论】:
-
你是专门要求 Rails 3 的吗(我从
.where得到提示)?
标签: ruby-on-rails sum rails-activerecord