Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ActiveRecord::Calculations returns incorrect data when grouping by multiple fields #704

Closed
lighthouse-import opened this Issue · 5 comments

2 participants

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/5182
Created by Alex - 2011-02-17 08:07:20 UTC

The :group option is broken for multiple fields when performing an AR calculation. See the example below:

Account.count(:all, :group => 'firm_id, credit_limit') # returns <OrderedHash {60=>1, 55=>1, 50=>1, 53=>1}>

The example above demonstrates that the returned hash incorrectly excludes data rows and columns. See below for the expected return value:

Account.count(:all, :group => 'firm_id, credit_limit') # should return <OrderedHash {[2, 60]=>1, [nil, 50]=>1, [9, 53]=>1, [6, 55]=>1, [1, 50]=>1, [6, 50]=>1}>

I have a attached a patch (test included) for supporting grouping by multiple fields. The behavior for grouping by a single field or an association is unchanged. Also, the :group option still takes in a string as always.

@lighthouse-import

Imported from Lighthouse.
Comment by Sebastian - 2010-09-11 02:27:33 UTC

I just ported your patch to 3-0-stable: #497

@lighthouse-import

Imported from Lighthouse.
Comment by Jeff Kreeftmeijer - 2010-11-08 08:53:01 UTC

Automatic cleanup of spam.

@lighthouse-import

Imported from Lighthouse.
Comment by Alex - 2010-11-18 18:39:14 UTC

This has been resolved, see #497 https://rails.lighthouseapp.com/projects/8994/tickets/497-activerecord-calculate-broken-for-multiple-fields-in-group-option

@steakchaser

I am using activerecord 3.2.14 and just experienced this issue when I included two fields in the same group statement:
group('id, another_table.field')

Placing these in two separate group statements then produced the correct count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.