Building a hash from query results

I need to build a hash out of the results of a query.

I had something like this in mind:

questions = survey_questions.find( params[:yaddayadda]) myHash = hash.new

for questions.each do |question| foo = question.some_field1 bar = question.some_field2 myHash[foo] = bar end

I’m guessing there is a BetterWay™. Any ideas?

Use inject

@questions.inject({}) do |memo, question|   memo.update question.some_field1 => question.some_field2 end

Wow, really nice trick, thanks a lot, that will really help. I’m trying the implementation now, thanks much!