gregbell/active_admin — pull request #1563 by pcreux (2012-07-27 14:52:01+00:00)
Title: Fix collections with group by

This pull request follows: #1555

This pull request works around this ActiveRecord bug: https://github.com/rails/rails/issues/7121

When #count is called on an ActiveRecord::Relation with custom `select` and `group` the custom `select` statement is dropped on the floor leading to SQL errors. For instance:

```ruby
Post.select("author, count(views) as total_views").group(:author).order("total_views desc").count
```

will run the SQL query:

```sql
SELECT COUNT(*) AS count_all, author AS author
FROM "posts"
GROUP BY author
ORDER BY "total_views" desc
```

which raises:

```
PG::Error: ERROR: column "total_views" must appear in the GROUP BY clause or be used in an aggregate function
```

To prevent this error, we call `reorder("")` to get rid of the `ORDER BY` clause. Changing the order does not affect the result of a count.

Please note that this error occurs on Postgres but does not occur on Sqlite.