Skip to content


Subversion checkout URL

You can clone with
Download ZIP


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

lighthouse-import opened this Issue · 5 comments

2 participants


Imported from Lighthouse. Original ticket at:
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.


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

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


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

Automatic cleanup of spam.


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

This has been resolved, see #497


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.