Creating arrays inside of hashes dynamically

I need to create a hash that has all of the objects of different product_id in arrays indexed by product_id as a key.

This gets close: fc.forecast_schedules.inject(Hash.new(0)) { |h, x| h[x.product_id] = ; h }

But the product_id in the hash only has the last forecast_schedule. I've tried things like this:

fc.forecast_schedules.inject(Hash.new(0)) { |h, x| h[x.product_id] << ; h }

But that gives me this error: TypeError: can't convert Array into Integer

How do I inform the hash key that holds something of the array instead of Integer....

-dustin

I figured out a solution using conditionals:

fc.forecast_schedules.inject(Hash.new(0)) { |h, schedule|   if h.has_key?(schedule.product_id)     h[schedule.product_id] << schedule   else     h[schedule.product_id] = [schedule]   end   h}

My original using conditionals looked like this: fc.forecast_schedules.inject(Hash.new(0)) { |h, schedule|   if h[schedule.product_id] == nil     h[schedule.product_id] = [schedule]   else     h[schedule.product_id] << schedule   end   h}

Why does the first one work but not the other?

This hurts my head.

-dustin