Help to get value from abject inside an array

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

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

On Sat, Mar 27, 2010 at 10:01 PM, Nicolas 2b [email protected]
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.

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

On Sat, Mar 27, 2010 at 3:01 PM, Nicolas 2b [email protected]
wrote:

What I want to do is iterate on the array and for each company_id, know

Posted via 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 [email protected]
wrote:

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

Posted via 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.