Joe_C
(Joe C)
1
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?
Rick_Olson
(Rick Olson)
2
Use inject
@questions.inject({}) do |memo, question|
memo.update question.some_field1 => question.some_field2
end
Joe_C
(Joe C)
3
Wow, really nice trick, thanks a lot, that will really help. I’m trying the implementation now, thanks much!