# Help to get value from abject inside an array

**URL:** <https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867>\
**Category:** Ruby\
**Created:** [March 27, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867 "2010-03-27T22:00:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolas2b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nicolas2b](https://www.ruby-forum.com/u/nicolas2b)\
**Post date:** [March 27, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867/1 "2010-03-27T22:00:54Z")

</div>

Hi,

My problem use RoR object but is ruby oriented so I post here.  
I got an array with lot of objects (type ‘Product’) like this (only 4  
here) :

[#\<Product id: 295147, company\_id: 1\>, #\<Product id: 303667, company\_id:  
2\>, #\<Product id: 279561, company\_id: 9\>, #\<Product id: 289477,  
company\_id: 4\>]

What I want to do is iterate on the array and for each company\_id, know  
how much products have the same company\_id.  
I get the company\_id like this : products.collect(&company\_id).uniq

but after I don’t know how to get an array like this : [1 =\> 22, 2 =\>  
15, 3 =\> 9]

Thanks for your help

* * *

Nicolas

---

<div class="post-metadata">

**Author:** ![nicolas2b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nicolas2b](https://www.ruby-forum.com/u/nicolas2b)\
**Post date:** [March 27, 2010, 10:04pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867/2 "2010-03-27T22:04:06Z")

</div>

Sorry for the errors in the title, if someone could correct them :S

---

<div class="post-metadata">

**Author:** ![nicolas2b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nicolas2b](https://www.ruby-forum.com/u/nicolas2b)\
**Post date:** [March 27, 2010, 10:11pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867/3 "2010-03-27T22:11:50Z")

</div>

On Sat, Mar 27, 2010 at 10:01 PM, Nicolas 2b [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> What I want to do is iterate on the array and for each company\_id, know  
> how much products have the same company\_id.  
> I get the company\_id like this : products.collect(&company\_id).uniq
> 
> but after I don’t know how to get an array like this : [1 =\> 22, 2 =\>  
> 15, 3 =\> 9]

I assume you want to get a hash that relates the companyId with the  
number of products of that company:

irb(main):016:0\> Product = Struct.new :companyId, :productId  
=\> Product  
irb(main):017:0\> a = [Product.new(1, 10), Product.new(1,11),  
Product.new(2,20), Product.new(3,30)]  
=\> [#, #, #, #]  
irb(main):018:0\> h = Hash.new(0)  
=\> {}  
irb(main):020:0\> a.each {|p| h[p[:companyId]] += 1}  
=\> [#, #, #, #]  
irb(main):022:0\> h  
=\> {1=\>2, 2=\>1, 3=\>1}

Jesus.

---

<div class="post-metadata">

**Author:** ![nicolas2b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nicolas2b](https://www.ruby-forum.com/u/nicolas2b)\
**Post date:** [March 28, 2010, 1:47pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867/4 "2010-03-28T13:47:23Z")

</div>

Nicolas 2b wrote:

> My problem use RoR object but is ruby oriented so I post here.  
> I got an array with lot of objects (type ‘Product’) like this (only 4  
> here) :
> 
> [#\<Product id: 295147, company\_id: 1\>, #\<Product id: 303667, company\_id:  
> 2\>, #\<Product id: 279561, company\_id: 9\>, #\<Product id: 289477,  
> company\_id: 4\>]
> 
> What I want to do is iterate on the array and for each company\_id, know  
> how much products have the same company\_id.  
> I get the company\_id like this : products.collect(&company\_id).uniq
> 
> but after I don’t know how to get an array like this : [1 =\> 22, 2 =\>  
> 15, 3 =\> 9]

company\_products = Hash.new(0)  
products.each { |p| company\_products[p.company\_id] += 1 }  
p company

---

<div class="post-metadata">

**Author:** ![nicolas2b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/3ab097/32.png) [@nicolas2b](https://www.ruby-forum.com/u/nicolas2b)\
**Post date:** [March 27, 2010, 10:55pm UTC](https://www.ruby-forum.com/t/help-to-get-value-from-abject-inside-an-array/185867/5 "2010-03-27T22:55:43Z")

</div>

On Sat, Mar 27, 2010 at 3:01 PM, Nicolas 2b [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## What I want to do is iterate on the array and for each company\_id, know
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

First, your syntax made it unclear what you wanted in the end, you said  
an  
array, but then used hash rockets to indicate key/value pairs. I’m not  
sure  
if I got it the way you are needing it.

* * *

Probably, you should let the database do this for you. Since I don’t  
know  
how you are using it, I can’t say for sure, but it sounds like you  
should be  
using the SQL count function. The Rails ML will be better equipped to  
help  
you.

products = Product.all :select =\> ‘company\_id , count(company\_id)’ ,  
:group  
=\> ‘company\_id’  
products.each do |product|  
company\_id = product.company\_id  
count = product.attributes[‘count(company\_id)’]  
puts “#{company\_id} = #{count}”  
end

I’m not sure if there is a nicer way to pull this out than using the  
attributes hash, but a quick test I ran had it working for me.

* * *

If using sql isn’t the best option for you, you can do something like  
this

product\_counts = products.group\_by do |product|  
product.company\_id  
end

product\_counts.each do |company\_id,products|  
product\_counts[company\_id] = products.size  
end

p product\_counts

On Sat, Mar 27, 2010 at 3:04 PM, Nicolas 2b [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ## Sorry for the errors in the title, if someone could correct them :S
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

The forum is just an interface to a mailing list, you can’t unsend email  
(well… not really), so you can’t correct them.
