Rails / Postgres: “must appear in the GROUP BY clause or be used in an aggregate function”

前端 未结 3 451
刺人心
刺人心 2021-01-19 05:41

I\'m using this method:

  def self.lines_price_report(n)
    Income.group(\'date(filled_at)\').having(\"date(filled_at) > ?\", Date.today - n).sum(:lines_         


        
3条回答
  •  天涯浪人
    2021-01-19 06:30

    I guess this is because you use date(filled_at) in GROUP BY but just filled at in ORDER. As I guess order is taken from default scope you need to overwrite it by reorder. I would suggest:

    Income.sum(:lines_price).
        group('date(filled_at)').
        having("date(filled_at) > ?", Date.today - n).
        reorder("date(filled_at) ASC")
    

提交回复
热议问题