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

Ticket #9711 (closed defect: incomplete)

Opened 1 year ago

Last modified 1 year ago

[PATCH] has_many :through, :uniq => true should calculate with :distinct

Reported by: tarmo Assigned to: core
Priority: normal Milestone: 1.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

Currently if you have a "has_many :through", ":uniq => true" association and call sum() or average() on it the ":uniq => true" part is ignored, this is counterintuitive and in most cases probably a serious error (at least it was in my app). You can get around this by calling sum(.., :distinct => true) but I think it's better if :distinct => true is automatically added.

I've written a patch (with tests) that makes this happen and also allows the distinctness to be disabled with sum(.., :distinct => false).

Attachments

has_many_through_uniq_calculations.patch (2.9 kB) - added by tarmo on 09/27/07 14:55:08.

Change History

09/27/07 14:55:08 changed by tarmo

  • attachment has_many_through_uniq_calculations.patch added.

09/28/07 11:23:11 changed by tarmo

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

Just realized that this isn't as simple as it seemed at first, :distinct only applies to the values being summed/averaged not to the records (or record id's). A subselect seems to be needed to make this work and I'm not sure how easy it would be to add such a feature.