# Collections of specific attribute values

**URL:** <https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163>\
**Category:** Rails\
**Created:** [November 30, 2005, 9:35pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163 "2005-11-30T21:35:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [November 30, 2005, 9:35pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163/1 "2005-11-30T21:35:29Z")

</div>

All,

Let’s say I have a collection of Address ActiveRecord instances - and  
let’s say that each Address has the property “zip”. What’s the best  
rails/ruby idiom for retrieving a collection of these zips?

Of course I can just iterate over my Addresses and pull out the zip but,  
as OCD as it sounds, I’m convinced that there’s something in the API  
that’ll let me say “collection.eachProperty(‘zip’)” or something along  
those lines.

Thanks for any help you can provide,  
Cory W.

---

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [November 30, 2005, 9:58pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163/2 "2005-11-30T21:58:45Z")

</div>

On Wed, 2005-11-30 at 21:35 +0100, Cory W. wrote:

> Thanks for any help you can provide,  
> Cory W.

As far as I’m aware, the easiest way to do this is:

zipcodes = Address.find(:all).map{|address| address.zip }

- Jamie

---

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [November 30, 2005, 10:15pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163/3 "2005-11-30T22:15:05Z")

</div>

On Wed, Nov 30, 2005 at 09:35:29PM +0100, Cory W. wrote:

> Let’s say I have a collection of Address ActiveRecord instances - and  
> let’s say that each Address has the property “zip”. What’s the best  
> rails/ruby idiom for retrieving a collection of these zips?
> 
> Of course I can just iterate over my Addresses and pull out the zip but,  
> as OCD as it sounds, I’m convinced that there’s something in the API  
> that’ll let me say “collection.eachProperty(‘zip’)” or something along  
> those lines.

In ActiveSupport’s trunk there is Symbol#to\_proc. It allows you to do:

> > User.find(:all).map(&:first\_name)  
> > =\> [“Marcel”, “Sam”, “David”, “Nicholas”, “Jeremy”, “Jamis”]

This is equivalent to the more verbose:

User.find(:all).map {|user| user.first\_name}

In your case:

addresses.map(&:zip)

Word of warning: zip is a method of class Array. If you accidentally  
call  
that on your collection, rather than on one of the records you’ll get a  
surprise.

marcel

---

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [November 30, 2005, 10:15pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163/4 "2005-11-30T22:15:12Z")

</div>

Similarly, if you have an array (here named ‘addresses’) of Address  
object _instances_, you can get the same result:

zipcodes = addresses.map { |address| address.zip }

---

<div class="post-metadata">

**Author:** ![corywilkerson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/aca169/32.png) [@corywilkerson](https://www.ruby-forum.com/u/corywilkerson)\
**Post date:** [November 30, 2005, 10:38pm UTC](https://www.ruby-forum.com/t/collections-of-specific-attribute-values/49163/5 "2005-11-30T22:38:07Z")

</div>

> If you accidentally call that on your collection, rather than on one of  
> the records you’ll get a surprise.
> 
> marcel

My first attempt was to do such a thing - you’re right - executed nicely

- but didn’t return exactly what I was after 😉

Everyone - thanks for the pointer. VERY helpful. I just _knew_ there  
had to be a slick construct for this. Ridiculous how Ruby always pulls  
through.

Sincerely,  
Cory
