# Activerecord and counting

**URL:** <https://www.ruby-forum.com/t/activerecord-and-counting/125766>\
**Category:** Rails\
**Created:** [January 11, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766 "2008-01-11T21:34:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [January 11, 2008, 9:34pm UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766/1 "2008-01-11T21:34:13Z")

</div>

hello all,

so i have the following situation

company has\_many products  
products has many parts

i can do company.products .count and get back the product count

what i’d like to know is if there is a more elegant way to get the  
number of parts for a given company outside of iterating through each  
product, counting its parts and then summing that number.

the solution i have now works. i was just wondering if there was a more  
elegant way of going about it.

thanks,

binh

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [January 11, 2008, 9:40pm UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766/2 "2008-01-11T21:40:25Z")

</div>

Here’s one way to do it:

Part.count :conditions =\> [‘product\_id in (select id from products  
where company\_id = ?)’, company\_id]

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [January 12, 2008, 12:06am UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766/3 "2008-01-12T00:06:18Z")

</div>

wouldn’t the counter\_cache improve your performance?

> **[\#23 Counter Cache Column - RailsCasts](http://railscasts.com/episodes/23-counter-cache-column)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

at least for having only to read the parts\_count from the product  
where company\_id = ?

select parts\_count from prodcuts where company\_id = ?

and after that sum them up 😉

should be one statement and an sum up. optimization possible.

that’s how i would try to do it 😉

On Jan 11, 9:45 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [January 11, 2008, 9:45pm UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766/4 "2008-01-11T21:45:37Z")

</div>

Binh Ly wrote:

> hello all,
> 
> so i have the following situation
> 
> company has\_many products  
> products has many parts
> 
> i can do company.products .count and get back the product count
> 
> what i’d like to know is if there is a more elegant way to get the  
> number of parts for a given company outside of iterating through each  
> product, counting its parts and then summing that number.
> 
> the solution i have now works. i was just wondering if there was a more  
> elegant way of going about it.
> 
> thanks,
> 
> binh

Within rails? probably not.

You could, however, create a view in the target database and then use  
that to return the desired values. I

Or, if this type of reporting is a frequent requirement, and the the  
parts per product is usually stable, then it might be worth while to  
have a regularly scheduled or dynamically calculated roll-up of the part  
count into a field in the product table and then report on that.

---

<div class="post-metadata">

**Author:** ![binkly](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@binkly](https://www.ruby-forum.com/u/binkly)\
**Post date:** [January 12, 2008, 12:35am UTC](https://www.ruby-forum.com/t/activerecord-and-counting/125766/5 "2008-01-12T00:35:32Z")

</div>

On Jan 11, 12:34 pm, Binh Ly [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> product, counting its parts and then summing that number.  
> Give Company a has\_many :through relation to Part, and then you’ll be  
> able to do company.parts.count.

///ark
