Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试进入 Rails3 的思维模式,但遇到了一个有趣的问题。
我有以下结构:
我想得到每个用户所有分数的总和,我怀疑范围可能有用......
这应该是您所需要的:
class User < ActiveRecord::Base has_many :tasks has_many :assets, through: :tasks def score_sum assets.pluck(:score).inject(:+) end # pre rails 3.2, try this: def score_sum assets.select(:score).map(&:score).inject(:+) end end