Let''s say I get a list of all places from the database in the controller: @places=Place.find(:all, :order=>"category_id") And in the view I want to list them under category headings Now let''s imagine categories is a hash like this {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} The only way I can think of doing it is by traversing the whole list for every category, ie: <% categories.each do | key,value | %> <%= value %><br /> <% @places.each do | place | %> <%= place.title if place.category_id==key %> <% end %> <% end %> Is there a better way of doing this that doesn''t involve traversing the entire array so many times? Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bsytlz
2008-Feb-03 03:44 UTC
Re: simple efficiency question: repeated iteration in the view
On Feb 1, 2:01 pm, AJS <andrewstaff...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Let''s say I get a list of all places from the database in the > controller: > @places=Place.find(:all, :order=>"category_id") > > And in the view I want to list them under category headings > > Now let''s imagine categories is a hash like this > {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} > by > The only way I can think of doing it is by traversing the whole list > for every category, ie: > > <% categories.each do | key,value | %> > <%= value %><br /> > <% -FMKtkPxneQ1bj60gHklGcw@public.gmane.org do | place | %> > <%= place.title if place.category_id==key %> > <% end %> > <% end %> > > Is there a better way of doing this that doesn''t involve traversing > the entire arraytwo ideas are: 1) execute 1 find for each category in the controller and pass the results to the view as separate collections. Can''t be sure of the relative perfomance though. 2) use a CASE or if/else if block in the view to presort the places into Collections on singlepass then render each collection under the apporopriate category heading. This should be faster then traversing the array multiple times but may clutter up you view logic Brian --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Rick Olson
2008-Feb-03 04:55 UTC
Re: simple efficiency question: repeated iteration in the view
On Feb 1, 2008 2:01 PM, AJS <andrewstaffell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Let''s say I get a list of all places from the database in the > controller: > @places=Place.find(:all, :order=>"category_id") > > And in the view I want to list them under category headings > > Now let''s imagine categories is a hash like this > {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} > > The only way I can think of doing it is by traversing the whole list > for every category, ie: > > <% categories.each do | key,value | %> > <%= value %><br /> > <% @places.each do | place | %> > <%= place.title if place.category_id==key %> > <% end %> > <% end %> > > Is there a better way of doing this that doesn''t involve traversing > the entire array so many times?use #group_by. It takes your array of @places and creates a structure like this: {0 => [<place>, <place>], 1 => [<place>], ...} @places=Place.find(:all, :order=>"category_id") @category_places = @places.group_by { |p| p.category_id } @categories = {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} @categories.each do |category_id, category| puts category @category_places[category_id].each do |place| puts place end 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 -~----------~----~----~----~------~----~------~--~---
Rick - brilliant. Thanks. On Feb 3, 4:55 am, "Rick Olson" <technowee...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 1, 2008 2:01 PM, AJS <andrewstaff...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Let''s say I get a list of all places from the database in the > > controller: > > @places=Place.find(:all, :order=>"category_id") > > > And in the view I want to list them under category headings > > > Now let''s imagine categories is a hash like this > > {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} > > > The only way I can think of doing it is by traversing the whole list > > for every category, ie: > > > <% categories.each do | key,value | %> > > <%= value %><br /> > > <% @places.each do | place | %> > > <%= place.title if place.category_id==key %> > > <% end %> > > <% end %> > > > Is there a better way of doing this that doesn''t involve traversing > > the entire array so many times? > > use #group_by. It takes your array of @places and creates a structure > like this: > > {0 => [<place>, <place>], 1 => [<place>], ...} > > @places=Place.find(:all, :order=>"category_id") > @category_places = @places.group_by { |p| p.category_id } > @categories = {0=>''Museum'',1=>''Gallery'',2=>''Historic House''} > > @categories.each do |category_id, category| > puts category > @category_places[category_id].each do |place| > puts place > end > end > > -- > Rick Olsonhttp://lighthouseapp.comhttp://weblog.techno-weenie.nethttp://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 -~----------~----~----~----~------~----~------~--~---