I have the table:
+----+----------------------+---------------------+
| 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 |
+----+----------------------+---------------------+
When I query this directly via MySQL it works perfectly as I want
select date(created_at) as date, sum(implemented_features) as sum from summaries group by date(created_at);
But when I try to convert this query to ActiveRecord syntax it returns me 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>]>
As you see, final queries are equal in both examples.
Why it don't work in case of ActiveRecord?
Using Rails 4.0.0, Ruby 2.0, mysql db in my rails project.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…