# Array of Objects

**URL:** <https://www.ruby-forum.com/t/array-of-objects/119742>\
**Category:** Rails\
**Created:** [November 9, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/array-of-objects/119742 "2007-11-09T21:33:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [November 9, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/array-of-objects/119742/1 "2007-11-09T21:33:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [November 9, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/array-of-objects/119742/2 "2007-11-09T21:50:40Z")

</div>

I am admittedly a Rails newbie, but wouldn’t the following code work:  
some\_survey\_types = Survey.find\_by\_type(“Some\_Survey\_Type”)

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [November 9, 2007, 9:58pm UTC](https://www.ruby-forum.com/t/array-of-objects/119742/3 "2007-11-09T21:58:51Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [November 9, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/array-of-objects/119742/4 "2007-11-09T22:50:20Z")

</div>

On 11/9/07, Joe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jcernelli](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/73ab20/32.png) [@jcernelli](https://www.ruby-forum.com/u/jcernelli)\
**Post date:** [November 10, 2007, 2:03am UTC](https://www.ruby-forum.com/t/array-of-objects/119742/5 "2007-11-10T02:03:54Z")

</div>

Thanks a ton, I was a hair off.
