Ruby on Rails按列和总和分组

时间:2015-12-01 15:12:14

标签: ruby-on-rails ruby activerecord

我的Rails应用程序中有以下Ruby代码:

# 1
data.pluck(:total_earnings).sum

# 2
data2 = data.select("restaurant, food_type, city, SUM(total_earnings) as total_earnings")
          .group("restaurant, food_type, city")

data2.pluck("total_earnings").sum

datadata2是ActiveRecord关系。

第1行给出了比第2行更大的值。 我无法理解为什么会这样,因为我希望每个total_earningsrestaurantfood_type组合获得city值。由于我将这些值相加,汇总数据,为什么我在这里丢失值?

1 个答案:

答案 0 :(得分:0)

如果没有看到你的桌面架构,可能很难说。您data.select...忽略total_earnings列的任何可能性

相关问题