Hi everyone! :) I''m currently accomplishing what I want, but I''d like to clean it up a bit. I''ve placed the following items in an array (please excuse my lack of proper syntax): @widgets [1, red] [2, blue] [3, red] [4, blue] I''d like to learn a way to search through the @widgets variable an separate the widgets, so that I could have: @blue_widgets [2, blue] [4, blue] @red_widgets [1, red] [4, blue] ...after performing a query on @widgets. I''m accomplishing this now by doing multiple queries to my database, but I''m sure there''s a way to do it by just doing one query, and then separating the data into different variables in my controller. Would that be the best place to do this? As always, any help is greatly appreciated. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Steve Castaneda wrote:> @red_widgets > > [1, red] > [4, red]Fixed the above - should of said "red". -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Steve Castaneda wrote:> Hi everyone! :) > > I''m currently accomplishing what I want, but I''d like to clean it up a > bit. I''ve placed the following items in an array (please excuse my lack > of proper syntax): > > @widgets > > [1, red] > [2, blue] > [3, red] > [4, blue] > > I''d like to learn a way to search through the @widgets variable an > separate the widgets, so that I could have: > > @blue_widgets > > [2, blue] > [4, blue] > > @red_widgets > > [1, red] > [4, blue] > > ...after performing a query on @widgets. > > I''m accomplishing this now by doing multiple queries to my database, but > I''m sure there''s a way to do it by just doing one query, and then > separating the dataLook at Array#partition.> into different variables in my controller. Would > that be the best place to do this?It might be better in the model. You shouldn''t have much logic in the controller. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Marnen Laibow-Koser wrote:> Look at Array#partition. > >> into different variables in my controller. Would >> that be the best place to do this? > > It might be better in the model. You shouldn''t have much logic in the > controller. > > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.orgThanks as always for the guidance, Marnen. I''m checking that out now. -- Posted via http://www.ruby-forum.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jan 20, 2010, at 6:59 PM, Steve Castaneda wrote:> Marnen Laibow-Koser wrote: >> Look at Array#partition. >> >>> into different variables in my controller. Would >>> that be the best place to do this? >> >> It might be better in the model. You shouldn''t have much logic in >> the >> controller. >> >> >> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > > Thanks as always for the guidance, Marnen. I''m checking that out now.And if you have more than just red/blue (or any two-choice condition), you might have more success with Enumerable#group_by (as added by ActiveSupport). Where Array#partition gives you a two-element array, Enumerable#group_by gives you a hash with the "condition" as the key and an array of elements as the value. irb> require ''rubygems'' => true irb> require ''activesupport'' => true irb> widgets = [ [1, ''red''], [2, ''blue''], [3, ''red''], [4, ''blue''], ] => [[1, "red"], [2, "blue"], [3, "red"], [4, "blue"]] irb> widgets.partition {|number,color| color == ''blue'' } => [[[2, "blue"], [4, "blue"]], [[1, "red"], [3, "red"]]] irb> widgets.group_by {|number, color| color } => #<OrderedHash {"blue"=>[[2, "blue"], [4, "blue"]], "red"=>[[1, "red"], [3, "red"]]}> (OK, so you actually get an OrderedHash from ruby1.8.6, but it works like you''d expect.) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.