我在Rails應用程序有這樣的SELECT語句:Rails Active Record`group`:爲什麼這隻返回幾列?
...
#<DailyStat subject: "Macroeconomics", topic: "Overview Of Economics", item_count: 160, correct_item_count: 0>,
#<DailyStat subject: "Macroeconomics", topic: "Overview Of Economics", item_count: 1, correct_item_count: 0>,
#<DailyStat subject: "Macroeconomics", topic: "Unemployment", item_count: 1, correct_item_count: 0>,
#<DailyStat subject: "Macroeconomics", topic: "Inflation", item_count: 2, correct_item_count: 0>,...
我得到這個,當我做一個基本的選擇這些列。現在,我的問題是,當我做daily_stats.select("subject, topic, sum(correct_item_count), sum(item_count)").group(:topic)
聚集列至於item_counts和correct_item_counts結合起來,所得到的結果集爲:
#<DailyStat topic: "Demand And Consumer Behavior">, #<DailyStat topic: "Obligations">, #<DailyStat topic: "Computerized Auditing">, #<DailyStat topic: "Inflation">, #<DailyStat topic: "Overview Of Economics">
我失去了計數。這肯定只是某種語法問題,但我遇到了麻煩。我如何得到計數加起來?
謝謝!但是,如何提取計數。我有一個後續問題。然後,我會如何將數組重新組織爲[subject] => [topic ...]? –
@DaryllSantos,道歉,不知道我是否關注你。我建議你爲跟進問題創建一個新問題。我認爲最好儘可能將問題保留在一個特定的主題上。 – vee
是的,當談到區分問題時你是對的。我要問的是按主題對結果行進行分組,但我已經解決了它。非常感謝你的回答,歡呼聲 –