I have a collection of objects of differing types, and I want to grab all of one type of object into another collection. Would this be roughly the correct way to do it: surveys = Survey.find(:all) some_survey_types = surveys.collect{|survey| survey.type ="Some_Survey_Type"} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I am admittedly a Rails newbie, but wouldn''t the following code work: some_survey_types = Survey.find_by_type("Some_Survey_Type") On 11/9/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a collection of objects of differing types, and I want to grab all of > one type of object into another collection. Would this be roughly the > correct way to do it: > > surveys = Survey.find(:all) > some_survey_types = surveys.collect{|survey| survey.type => "Some_Survey_Type"} > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi, thanks for the response. Yes, that works, but I already have the collection in memory, and don''t want to hit the database needlessly with another find. In my example I used the find just to generally show what the collection is, and as a result over simplified it! On Nov 9, 2007 3:49 PM, Paulo Schneider <paulo.schneider-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I am admittedly a Rails newbie, but wouldn''t the following code work: > some_survey_types = Survey.find_by_type("Some_Survey_Type") > > On 11/9/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have a collection of objects of differing types, and I want to grab > all of > > one type of object into another collection. Would this be roughly the > > correct way to do it: > > > > surveys = Survey.find(:all) > > some_survey_types = surveys.collect{|survey| survey.type => > "Some_Survey_Type"} > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 11/9/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a collection of objects of differing types, and I want to grab all of > one type of object into another collection. Would this be roughly the > correct way to do it: > > surveys = Survey.find(:all) > some_survey_types = surveys.collect{|survey| survey.type => "Some_Survey_Type"}No, you''ll end up with an array of boolean values. Try some_survey_types = surveys.select {|survey| survey.type == "Some_Survey_Type"} -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---
Thanks a ton, I was a hair off. On Nov 9, 2007 4:49 PM, Rick DeNatale <rick.denatale-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 11/9/07, Joe Cairns <joe.cairns-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have a collection of objects of differing types, and I want to grab > all of > > one type of object into another collection. Would this be roughly the > > correct way to do it: > > > > surveys = Survey.find(:all) > > some_survey_types = surveys.collect{|survey| survey.type => > "Some_Survey_Type"} > > No, you''ll end up with an array of boolean values. > > Try > > some_survey_types = surveys.select {|survey| survey.type => "Some_Survey_Type"} > > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.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 -~----------~----~----~----~------~----~------~--~---