Howdy. I imagine this is an elementary Ruby question, but I''d love to learn the right Ruby idiom for this. I''d like to take the results of an ActiveRecord.find() and turn them into an array of arrays [[item1_col1,item1_col2], [item2_col1, item2_col2]]. Here''s my code sample of the brute force way I''m doing it, but I bet it can be reduced fewer lines of code... @crit_sections = Hash.new x = Reference.find( :all, :select => "refid,name", :conditions => "ref_type = ''Division''", :order => "name") ar = [] x.each do |xi| ar.push([xi.refid, xi.name]) end @crit_sections["division"] = ar Much thanks! Michael --~--~---------~--~----~------------~-------~--~----~ 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 Wed, Dec 17, 2008 at 9:08 AM, michael_teter <michael.teter-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Howdy. I imagine this is an elementary Ruby question, but I''d love to > learn the right Ruby idiom for this. > > I''d like to take the results of an ActiveRecord.find() and turn them > into an array of arrays [[item1_col1,item1_col2], [item2_col1, > item2_col2]]. > > Here''s my code sample of the brute force way I''m doing it, but I bet > it can be reduced fewer lines of code... > > @crit_sections = Hash.new > x = Reference.find( > :all, > :select => "refid,name", > :conditions => "ref_type = ''Division''", > :order => "name") >You can use the collect method @crit_sections[:division] = Reference.find(:all, :select => "refid,name", :conditions => "ref_type = ''Division''", :order => "name").collect { |r| [ r.refid, r.name ] } I took the liberty of using a symbol for your hash key instead of a string. Read up on symbols - in simplest terms, they are like strings without all the string functionality, which we usually don''t need anyway for hash keys. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That''s exactly what I was after. I knew it was possible, but I was missing "collect" :) As for symbols, at one point I used them a lot, but then I ran into some places that seemed to really want strings. Out of confusion, I stopped using symbols in a lot of cases. I will re-evaluate where I''m using strings though. Thanks! On Dec 17, 2:39 am, "Franz Strebel" <franz.stre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Wed, Dec 17, 2008 at 9:08 AM, michael_teter <michael.te...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Howdy. I imagine this is an elementary Ruby question, but I''d love to > > learn the right Ruby idiom for this. > > > I''d like to take the results of an ActiveRecord.find() and turn them > > into an array of arrays [[item1_col1,item1_col2], [item2_col1, > > item2_col2]]. > > > Here''s my code sample of the brute force way I''m doing it, but I bet > > it can be reduced fewer lines of code... > > > @crit_sections = Hash.new > > x = Reference.find( > > :all, > > :select => "refid,name", > > :conditions => "ref_type = ''Division''", > > :order => "name") > > You can use the collect method > > @crit_sections[:division] = Reference.find(:all, > :select => > "refid,name", > > :conditions => "ref_type = ''Division''", > :order => > "name").collect { |r| [ r.refid, r.name ] } > > I took the liberty of using a symbol for your hash key instead of a string. > Read up on symbols - in simplest terms, they are like strings without all > the string functionality, which we usually don''t need anyway for hash keys.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---