Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11502 (closed defect: fixed)

Opened 5 months ago

Last modified 5 months ago

[PATCH] Double DISTINCT in calculations with eager loading

Reported by: kenneth.kalmer Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

When calling #count on a model and passing a :select value with DISTINCT in, and eager loading an association, ActiveRecord blindly adds another DISTINCT to the COUNT parameter.

Example (based on ActiveRecord test cases)

Account.count( :select => 'DISTINCT accounts.id', :include => :firm )

Produces the following invalid SQL

SELECT count(DISTINCT DISTINCT accounts.id) AS count_distinct_accounts_id FROM `accounts`  LEFT OUTER JOIN `companies` ON `companies`.id = `accounts`.firm_id AND `companies`.`type` = 'Firm'

The fix is trivial, and all tests pass in the same fashion as before the patch. Hope its accepted quickly...

This was tested with MySQL, discovered by chance while debugging the paginated_finder plugin.

Attachments

fix_for_count_with_select_distinct_and_eager_loading.diff (1.3 kB) - added by kenneth.kalmer on 04/01/08 21:40:12.
Patch against r9205

Change History

04/01/08 21:40:12 changed by kenneth.kalmer

  • attachment fix_for_count_with_select_distinct_and_eager_loading.diff added.

Patch against r9205

04/02/08 07:03:38 changed by cavalle

+1 Tests pass

04/04/08 12:06:25 changed by nzkoz

  • status changed from new to closed.
  • resolution set to fixed.

(In [9223]) Don't double include DISTINCT when the user has already specified it. Closes #11502 [kenneth.kalmer]