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(tm). Any ideas? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 10/26/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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(tm). Any ideas?Use inject @questions.inject({}) do |memo, question| memo.update question.some_field1 => question.some_field2 end -- Rick Olson http://lighthouseapp.com http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Wow, really nice trick, thanks a lot, that will really help. I''m trying the implementation now, thanks much! On 10/26/07, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote:> > > On 10/26/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > wrote: > > 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(tm). Any ideas? > > Use inject > > @questions.inject({}) do |memo, question| > memo.update question.some_field1 => question.some_field2 > end > > > -- > Rick Olson > http://lighthouseapp.com > http://weblog.techno-weenie.net > http://mephistoblog.com > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---