The key hash is an optimization. It's a hash of hashes were the keys are columns, such as id, and the values are subhashes.
The subhashes have keys which are the values of the column, such as 1 or 2, and values which are an array of instances which have the related value for that column.
So in the example above, firm1 has id 1 and firm2 has id 2.
In this case, since the association depends on the firm's primary key, we only care about that specific subhash, which we assign to a local variable id_map.