rails group by multiple columns

前端 未结 3 1092
忘掉有多难
忘掉有多难 2021-01-11 13:59

i have budgets table with emptype_id and calendar_id actual_head, estimated_head

when i do Budgets.sum(:actual_head ,:group=>\"emptype_id,calendar_id\")

相关标签:
3条回答
  • 2021-01-11 14:27

    I'm not sure of this, buy try :group => [:emptype_id, :calendar_id]

    0 讨论(0)
  • 2021-01-11 14:30

    I cheat. Do :group => ["emptype_id,calendar_id"].

    Not want you nor I want, but this works at least.

    0 讨论(0)
  • 2021-01-11 14:32

    Grouping with multiple columns cannot be supported by rails. You have to use a regular find all:

    budgets = Budgets.find(:all, 
                           :select => "emptype_id, calendar_id, sum(budgets.actual_head) AS sum_actual_head", 
                           :group => "emptype_id, calendar_id")
    
    budgets.each { |budget| puts budget.sum_actual_head }
    
    0 讨论(0)
提交回复
热议问题