Rails Guide's ActiveQuery Group Example is Wrong?

Is it me, or does the ActiveQuery Rails Guide example for using group (link) not make sense?

To apply a GROUP BY clause to the SQL fired by the finder, you can specify the group method 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 Order object 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 Order.select('date(created_at)').distinct.

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).

@robwise It might not make sense to get Order objects when doing a group. What you really want is a table of values returned from the query.

Well then maybe I would do pluck, but either way, the docs are wrong, right?

Yes, I don’t think you get Order objects. You should be getting an array of Hashes with 2 attributes, ordered_date and total_price.

BTW, it doesn’t make a sense to have a price attribute on orders. You have an extension which is price * quantity, and it makes sense to sum per date to get total sales per date.

Negative, I actually created a test project from scratch to figure this out. All this does is return an ActiveRecord collection of Order objects, except they only have a single attribute id, and it’s set to nil. That was my point—this can’t be the intended behavior because it’s so useless.

1 Like

@robwise That’s, an awesome example of figuring out what’s really happening. There’s really 2 realities:

  1. What the doc says, along with Google/StackOverflow searches.
  2. What the console and source code say.

When you start off coding, it’s hard to speak “console” and “code”, and easier to go by docs and Google/StackOverflow.

As you get more experienced, you go more by what the code says.

There’s several things we can do with this issue:

  1. Submit a doc PR to Rails.
  2. Submit a bug report
  3. Submit a code PR to Rails.

Based on the observed behavior, possibly there’s a slightly different syntax to document? Or possibly there’s a bug.

I suspect that most Rails devs for a group_by would just be executing:

ActiveRecord::Base.connection.execute("SQL query") 

and getting back an array of Hashes.

The Rails bug/report contributing process just seems too complex to bother with in my opinion. I saw the one you wrote for the enum functionality and despite the doc being completely wrong and it not functioning as intended, they just said “yeah it’s broken we’ll fix it in Rails 5” and left the documentation like that. :confused: