First, I think a cleaner output structure is to allow user identifiers to be hash keys, and a list of search identifiers to be values:
{ 1 => [a, b], 2 => [c, d] }
There may be a reasonable way to use Rails helpers to get this structure, but it is not so bad to do it manually:
output = {} input.each do |row| key = row[:user_id] value = row[:search_id] output[key] ||= [] output[key] << value end
source share