Array of Objects

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”}

I am admittedly a Rails newbie, but wouldn’t the following code work:
some_survey_types = Survey.find_by_type(“Some_Survey_Type”)

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 11/9/07, Joe C. [email protected] 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/

Thanks a ton, I was a hair off.