Help to get value from abject inside an array

N

Nicolas 2b

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
 
J

Jesús Gabriel y Galán

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]

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)]
=> [#<struct Product companyId=1, productId=10>, #<struct Product companyId=1, p
roductId=11>, #<struct Product companyId=2, productId=20>, #<struct Product comp
anyId=3, productId=30>]
irb(main):018:0> h = Hash.new(0)
=> {}
irb(main):020:0> a.each {|p| h[p[:companyId]] += 1}
=> [#<struct Product companyId=1, productId=10>, #<struct Product companyId=1, p
roductId=11>, #<struct Product companyId=2, productId=20>, #<struct Product comp
anyId=3, productId=30>]
irb(main):022:0> h
=> {1=>2, 2=>1, 3=>1}

Jesus.
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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
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




Sorry for the errors in the title, if someone could correct them :S
The forum is just an interface to a mailing list, you can't unsend email
(well... not really), so you can't correct them.
 
B

Brian Candler

Nicolas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top