rails/rails — pull request #4738 by gregolsen (2012-01-28 22:14:58+00:00)
Title: Pluck fails when ordering by column that is not included in the SELECT DISTINCT query (PostgreSQL)

Don't know if it's correct behavior or not but here it goes:
PostgreSQL SELECT DISTINCT query fails if result is ordered by a column that is not included in the select list.

For example, this code:
```User.order(:first_name).uniq.pluck(:id)```
troughs this error (when using postresql):
```SELECT DISTINCT id FROM "users" ORDER BY first_name
ActiveRecord::StatementInvalid: PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
LINE 1: SELECT DISTINCT id FROM "users" ORDER BY first_name```

See first commit with test that fails on PostgreSQL.

That can be fixed by cleaning ```Arel::SelectManager#orders``` list just before 'plucking' (see the second commit).
To make MySQL users happy (MySQL allows to order by a column that is not in the select list) we can clean orders list only when PostgreSQL is used (I'm not sure about that, so it's not included in the PR):

```ruby
if select_manager.engine.connection.adapter_name ~= /postgresql/i
select_manager.orders.delete_if { |key| key != column_name }
end
```

That's a typical situation - having conditions like ```uniq: true``` on an association, along with the ```default_scope``` ordering by some column. Than 'plucking' on that association can lead to this error easily.

Let me know if it's desired behavior or we can make awesome ```pluck``` method even more user friendly.

Thanks!