0
我剛開始學習一些數據庫基礎知識。我使用紅寶石和DataMapper的寶石Datamapper:如何從has_many對象中計算總分
我有兩個簡單的對象:
class Quote
include DataMapper::Resource
property :id, Serial
property :saying, String, :required => true
property :score, Integer, :default => 5
belongs_to :user
end
和
class User
include DataMapper::Resource
property :id, Serial
has n, :quotes
end
不,我想獲得的該總比分用戶。總分是用戶所有相關報價分數的總和。
我想是這樣
@totalscore = @user.quotes.inject(0) {|count, q| count + q.score}
,但我想這不可能是我應該使用一個數據庫的方式,對不對?
任何幫助表示讚賞!
最佳,
託比
大,這個作品!謝謝! –