Is it me, or does the ActiveQuery Rails Guide example for using
group (link) not make sense?
To apply a
GROUP BYclause to the SQL fired by the finder, you can specify the
groupmethod on the find.
For example, if you want to find a collection of the dates orders were created on:
Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)")
And this will give you a single
Orderobject for each date where there are orders in the database.
The SQL that would be executed would be something like this:
SELECT date(created_at) as ordered_date, sum(price) as total_price FROM orders GROUP BY date(created_at)
This just gets an
ActiveRecord::Relation collection of objects with a single
id attribute set to
nil. What they’re saying they really want, “a collection of dates that the orders were created on,” would be easily achieved via
I’m starting to get paranoid about documentation after my troubles with Rails 4.x’s completely incorrect
enum documentation (Justin I saw your blog post on it only a week after I had my own hair-tearingly frustrating episode with it).