# Activerecord.count calculation not reurning expected output

**URL:** <https://www.ruby-forum.com/t/activerecord-count-calculation-not-reurning-expected-output/167489>\
**Category:** Rails\
**Created:** [May 8, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/activerecord-count-calculation-not-reurning-expected-output/167489 "2009-05-08T12:35:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 8, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/activerecord-count-calculation-not-reurning-expected-output/167489/1 "2009-05-08T12:35:07Z")

</div>

Hi,

Im using the count calculations in activerecord, see below

self.count(:id, :include =\> [:operating\_systems], :group =\>  
‘operating\_systems.name, operating\_systems.id’,

This only returns 2 of the fields i need the id and the count:

count\_id operating\_systems\_name\_operating\_systems\_id  
49 1  
30 2

If i run the sql this generates in the log on the database itself i get  
the follwoing

SELECT count(DISTINCT “products”.id) AS count\_id,  
operating\_systems.name, operating\_systems.id AS  
operating\_systems\_name\_operating\_systems\_id FROM “products” LEFT OUTER  
JOIN “product\_operating\_systems” ON (“products”.“id” =  
“product\_operating\_systems”.“product\_id”) LEFT OUTER JOIN  
“operating\_systems” ON (“operating\_systems”.“id” =  
“product\_operating\_systems”.“operating\_system\_id”) GROUP BY  
operating\_systems.name, operating\_systems.id

So the above sql run on the DB i get 3 columns with the required  
information:

count\_id name operating\_systems\_name\_operating\_systems\_id  
49 PC 1  
30 MAC 2

Is there any reason for this? Anybody got a better solution?

JB
