# Constructing an array of objects from an array of objects

**URL:** <https://www.ruby-forum.com/t/constructing-an-array-of-objects-from-an-array-of-objects/115531>\
**Category:** Rails\
**Created:** [September 26, 2007, 1:00am UTC](https://www.ruby-forum.com/t/constructing-an-array-of-objects-from-an-array-of-objects/115531 "2007-09-26T01:00:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 26, 2007, 1:00am UTC](https://www.ruby-forum.com/t/constructing-an-array-of-objects-from-an-array-of-objects/115531/1 "2007-09-26T01:00:42Z")

</div>

On line 1 I have constructed an array of Event objects, which belong\_to  
an Account object. How might I construct an array of Account objects  
from this array of Event objects on line 2?

1 notices = Event.find(:all, :conditions =\> [“account\_id IN (?) AND name  
= ?”, self.account\_ids, account\_notice\_sent’])  
2 return notices.account

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 26, 2007, 1:18am UTC](https://www.ruby-forum.com/t/constructing-an-array-of-objects-from-an-array-of-objects/115531/2 "2007-09-26T01:18:05Z")

</div>

Use collect:

accounts = notices.collect{|n| n.account}

You may want to add :include =\> ‘account’ to your Event.find to eager  
load the accounts.

Peter M. wrote:

> On line 1 I have constructed an array of Event objects, which belong\_to  
> an Account object. How might I construct an array of Account objects  
> from this array of Event objects on line 2?
> 
> 1 notices = Event.find(:all, :conditions =\> [“account\_id IN (?) AND name  
> = ?”, self.account\_ids, account\_notice\_sent’])  
> 2 return notices.account

–  
Sincerely,

William P.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 26, 2007, 2:50am UTC](https://www.ruby-forum.com/t/constructing-an-array-of-objects-from-an-array-of-objects/115531/3 "2007-09-26T02:50:02Z")

</div>

Exactly what I was looking for. Thanks William 🙂
