我有桌子:
+----+----------------------+---------------------+
| id | implemented_features | created_at |
+----+----------------------+---------------------+
| 1 | 19 | 2013-07-18 04:10:12 |
| 2 | 6 | 2013-07-18 04:10:12 |
| 3 | 26 | 2013-07-19 04:10:12 |
| 4 | 11 | 2013-07-19 04:10:12 |
| 5 | 1 | 2013-07-20 04:10:12 |
+----+----------------------+---------------------+
当我直接通过 MySQL 查询它时,它可以按我的意愿完美运行
select date(created_at) as date, sum(implemented_features) as sum from summaries group by date(created_at);
但是当我尝试将此查询转换为 ActiveRecord 语法时,它返回 nil。
2.0.0p0 :035 > Summary.select("date(created_at) as date, sum(implemented_features)").group("date(created_at)")
Summary Load (0.5ms) SELECT date(created_at) as date, sum(implemented_features) FROM `summaries` GROUP BY date(created_at)
=> #<ActiveRecord::Relation [#<Summary id: nil>]>
如您所见,两个示例中的最终查询是相同的。为什么它在 ActiveRecord 的情况下不起作用?
在我的 rails 项目中使用 Rails 4.0.0、Ruby 2.0、mysql db。