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?

On 10/26/07, Joe C. [email protected] wrote:

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


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

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