rails/rails — pull request #3572 by amatsuda (2011-11-08 20:18:44+00:00)
Title: exclude ORDER BY clause when querying Relation#exists?

Currently, there's a failing test case in activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb

```
PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list
```

The reason behind this is because it tries to check whether a record `exists?` over the following uniq habtm association:

```
Project.has_and_belongs_to_many :developers, :uniq => true, :order => 'developers.name desc, developers.id desc'
```

then it runs an invalid SQL like this

```
SELECT DISTINCT 1 FROM "developers" INNER JOIN (snip) ORDER BY developers.name desc, developers.id desc LIMIT 1
```

This pr fixes the `exists?` method to ORDER BY clause which is not needed in this case.