How to create associative hashs in Ruby (on Rails) dynamicly I am developing a webapp with Ruby on Rails to expand the features of mite (online time-tracking app) with the help of the offical mite API. Unfortuanaly, the API can't return all projects by a specific customer id. Because of that, i am developing a workaround.

At first, i get all customers and save them in a hash. Then i iterate throug all projects and save each project object to a hash inside the customer hash. 

My code looks like this:

def index
  # Mite authentication
  Mite.account = '...'
  Mite.authenticate('...','...')

  # get all customers from Mite
  @customers = Hash.new
  Mite::Customer.all.each do |customer|
    @customers[customer.id] = {:name =&gt; customer.name, :projects =&gt; Hash.new}
  end

  # get all projects from Mite
  Mite::Project.all.each do |project|
    @customers[project.customer_id][:projects] = {:project =&gt; project}
  end
end


Unfortuanaly, only the last project is saved in the ":projects" hash. But why? Adding customers works with this method too. In general, is this "the ruby way"? I'm very new to it :)
You should save projects in array or something like it, not hash. For example

  Mite::Customer.all.each do |customer|
    @customers[customer.id] = {:name =&gt; customer.name, :projects =&gt; []} # !!!
  end

  Mite::Project.all.each do |project|
    @customers[project.customer_id][:projects] &lt;&lt; project # !!!
  end