2017-06-22 39 views

回答

1

您可以使用sum

Model.sum(:amount) 

選擇日期,只需添加where

Model.where(date: from_date..to_date).sum(:amount) 
+0

Thaks加里,我想你有什麼建議,但是它不工作 ***一.account.where(日期:2017年2月27日,星期一,2017年3月31日,星期五).sum(:金額)*** SyntaxError:意外tCONSTANT,預計輸入結束 a.acco unt.where(日期:2017年2月27日,星期一,2017年3月31日,星期五).sum(:金額) –

+0

@rail_engine您需要使用DateTime對象或_yyyy-mm-dd_格式的字符串。例如,嘗試使用'a.account.where(date:「2017-02-27」..「2017-03-31」)。sum(:amount)'。 – Gerry

+0

非常感謝,現在工作正常。 –

相關問題