# Harvest field values to an array

**URL:** <https://www.ruby-forum.com/t/harvest-field-values-to-an-array/131334>\
**Category:** Rails\
**Created:** [March 8, 2008, 7:04am UTC](https://www.ruby-forum.com/t/harvest-field-values-to-an-array/131334 "2008-03-08T07:04:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 8, 2008, 7:04am UTC](https://www.ruby-forum.com/t/harvest-field-values-to-an-array/131334/1 "2008-03-08T07:04:20Z")

</div>

Hi-

Simple question, just can’t find a reference. I have an array of  
records via a habtm, and simply want to harvest all of the values of  
one field into an array of strings. Example:

story = Story.find(:first)  
comments = story.comments.XXXX # should equal [“great story”, “i  
agree”, …]

I suspect there is a simple built in method.

Thanks for any help,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 8, 2008, 8:35am UTC](https://www.ruby-forum.com/t/harvest-field-values-to-an-array/131334/2 "2008-03-08T08:35:43Z")

</div>

comments = story.comments.collect {|comment| comment.body }

assuming that Comment objects respond to a ‘body’ method which  
contains the meat and potatoes of the comment.

Mike
