# Getting id in an array

**URL:** <https://www.ruby-forum.com/t/getting-id-in-an-array/144395>\
**Category:** Rails\
**Created:** [July 29, 2008, 11:36am UTC](https://www.ruby-forum.com/t/getting-id-in-an-array/144395 "2008-07-29T11:36:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [July 29, 2008, 11:36am UTC](https://www.ruby-forum.com/t/getting-id-in-an-array/144395/1 "2008-07-29T11:36:57Z")

</div>

Hi,  
I’m begining with rails I need to get an array of all id of a table.

name of my model is ContentGroup so I tried ContentGroup.find(:all,  
:order=\>“ASC”)  
but I have an array of objet and I only need the ids.

How can I do to get only id fields ?

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [July 29, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/getting-id-in-an-array/144395/2 "2008-07-29T16:37:13Z")

</div>

David N. wrote:

> Hi,  
> I’m begining with rails I need to get an array of all id of a table.
> 
> name of my model is ContentGroup so I tried ContentGroup.find(:all,  
> :order=\>“ASC”)  
> but I have an array of objet and I only need the ids.
> 
> How can I do to get only id fields ?

Hi  
Try this  
@user=ContentGroup.find(:all,:select=\>“id”,:order=\>“id asc”)

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [July 29, 2008, 6:04pm UTC](https://www.ruby-forum.com/t/getting-id-in-an-array/144395/3 "2008-07-29T18:04:44Z")

</div>

On Tue, Jul 29, 2008 at 4:36 AM, David N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,  
> I’m begining with rails I need to get an array of all id of a table.
> 
> name of my model is ContentGroup so I tried ContentGroup.find(:all,  
> :order=\>“ASC”)  
> but I have an array of objet and I only need the ids.
> 
> How can I do to get only id fields ?

ContentGroup.find(:all, :order=\>“ASC”).collect {|cg| cg.id}

---

<div class="post-metadata">

**Author:** ![vanight](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a698b9/32.png) [@vanight](https://www.ruby-forum.com/u/vanight)\
**Post date:** [July 30, 2008, 3:51pm UTC](https://www.ruby-forum.com/t/getting-id-in-an-array/144395/4 "2008-07-30T15:51:27Z")

</div>

David N. wrote:

> Hi,  
> I’m begining with rails I need to get an array of all id of a table.
> 
> name of my model is ContentGroup so I tried ContentGroup.find(:all,  
> :order=\>“ASC”)  
> but I have an array of objet and I only need the ids.
> 
> How can I do to get only id fields ?

ContentGroup.find(:all, :select =\> ‘id’).map(&:id)
