# Using the collect method to collect to a 2 dimensional array

**URL:** <https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188>\
**Category:** Rails\
**Created:** [September 10, 2008, 8:24am UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188 "2008-09-10T08:24:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 10, 2008, 8:24am UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/1 "2008-09-10T08:24:08Z")

</div>

Hi  
I am trying to collect name and email of a person to an array (Or  
hash?) like  
[[name1,emailid],[name2,emailid]…] But I dont know how to do that.  
I tried like

email\_array =  
Contact.get\_email\_id\_and\_name\_of\_contacts(problem.reported\_by\_id) unless  
problem.reported\_by\_id.blank?

And in Contact class

def self.get\_email\_ids\_of\_contacts(contact\_id)  
return Contact.find\_by\_id(contact\_id).name,  
Contact.find\_by\_id(contact\_id).contact\_email\_addresses.find(:all,  
:conditions =\> [‘contact\_email\_address\_type\_id=?’,2]).collect { |mail|  
mail.email}  
end

```
  Here I tried to return two value at a time .But not suceeded .How

```

can I do that?Please help

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 10, 2008, 8:56am UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/2 "2008-09-10T08:56:05Z")

</div>

On 10 Sep 2008, at 07:24, Sijo Kg wrote:

> unless

Collect just bungs whatever your block evaluates to in an array. If  
that block evaluates to an array then you’ll get an array of arrays  
back.

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 10, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/3 "2008-09-10T18:20:59Z")

</div>

Sijo,

not sure, but looking at your code it seems you may want to do something  
like the following

def self.get\_email\_ids\_of\_contacts(contact\_id)  
contact = Contact.find\_by\_id(contact\_id)

contact.contact\_email\_addresses.find(:all, :conditions =\> []).map do  
|mail|  
[contact.name, mail.email]  
end  
end

Sijo Kg wrote:

> def self.get\_email\_ids\_of\_contacts(contact\_id)  
> return Contact.find\_by\_id(contact\_id).name,  
> Contact.find\_by\_id(contact\_id).contact\_email\_addresses.find(:all,  
> :conditions =\> [‘contact\_email\_address\_type\_id=?’,2]).collect { |mail|  
> mail.email}  
> end

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 10:07am UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/4 "2008-09-11T10:07:38Z")

</div>

On Sep 11, 6:28 am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> But this does not give array like above?Could you please tell me  
> where is the fault?A group may have more than one manager and each  
> manager has more than one email addresses of the above type.That is why  
> I wrote code like above Groups and Contacts are connected through user  
> groups (has\_many :through relation)

You’re generating these arrays using map but then you’re just throwing  
them away (each returns the collection it iterated over. Perhaps you  
mean that to be map or inject instead to concatenate the various  
arrays produced?

Fred

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 7:28am UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/5 "2008-09-11T07:28:49Z")

</div>

Hi  
Lots of thanks for your great reply.This is what i needed.Now it  
works. But I have one more problem. I have to find group managers name  
and email like above and I tried like below  
name\_and\_email\_managers=Group.get\_managers\_name\_and\_emails(problem.primary\_assignee\_group\_id)  
unless problem.primary\_assignee\_group\_id.blank?

And now in Group class  
def self.get\_managers\_name\_and\_emails(grp\_id)  
managers=Group.find\_by\_id(grp\_id).contacts.find(:all,:conditions =\>  
[‘group\_user\_type\_id = ?’,4])  
managers.each do |manager|  
manager.contact\_email\_addresses.find(:all, :conditions =\>  
[‘contact\_email\_address\_type\_id=?’,2]).map do |mail|  
[manager.name,mail.email]  
end  
end  
end  
But this does not give array like above?Could you please tell me  
where is the fault?A group may have more than one manager and each  
manager has more than one email addresses of the above type.That is why  
I wrote code like above Groups and Contacts are connected through user  
groups (has\_many :through relation)

Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [September 11, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/using-the-collect-method-to-collect-to-a-2-dimensional-array/148188/6 "2008-09-11T17:17:35Z")

</div>

Frederick C. wrote:

> On Sep 11, 6:28ï¿½am, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > end  
> > ï¿½ ï¿½ ï¿½ ï¿½ But this does not give array like above?Could you please tell me  
> > where is the fault?A group may have more than one manager and each  
> > manager has more than one email addresses of the above type.That is why  
> > I wrote code like above ï¿½Groups and Contacts are connected through user  
> > groups (has\_many :through relation)
> 
> You’re generating these arrays using map but then you’re just throwing  
> them away (each returns the collection it iterated over. Perhaps you  
> mean that to be map or inject instead to concatenate the various  
> arrays produced?
> 
> Fred

Array.partial
