# Model.find(:all) - return a list of ids

**URL:** <https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858>\
**Category:** Rails\
**Created:** [April 12, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858 "2007-04-12T22:03:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/1 "2007-04-12T22:03:14Z")

</div>

Hi,

I currently call find(:all, …) on one of my models and it return a  
page of models.

I am now looking at adding a memcached server and want the find(:all…)  
to only return model ids, the models will be individually retrieved from  
the cache.

Is this something that ActiveRecord supports or should I write a custom  
bit of sql?

Thanks,  
GiantCranes

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/2 "2007-04-12T22:34:11Z")

</div>

On 4/12/07, Giant C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is this something that ActiveRecord supports or should I write a custom  
> bit of sql?

You’ll have to write a bit of SQL for this, since it’s not really a  
common operation. You can use the #select\_values method of the  
connection object:

Widget.connection.select\_values(“select id from widgets”)

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/3 "2007-04-12T22:35:32Z")

</div>

Thanks Rick, I’ll do just that then.

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:43pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/4 "2007-04-12T22:43:11Z")

</div>

> I currently call find(:all, …) on one of my models and it  
> return a page of models.
> 
> I am now looking at adding a memcached server and want the  
> find(:all…) to only return model ids, the models will be  
> individually retrieved from the cache.
> 
> Is this something that ActiveRecord supports or should I  
> write a custom bit of sql?

You can do custom sql, or use the :select option to limit the columns  
returned:

model.find(:all, :select=\>‘id’) returns an array of models with only the  
ids loaded

model.find(:all, :select=\>‘id’).collect(&:id) returns an array of ids

if you like using the rest of the find options to help generate your sql  
for you.

- donald

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:55pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/5 "2007-04-12T22:55:20Z")

</div>

> > You can do custom sql, or use the :select option to limit the columns  
> > returned:
> > 
> > model.find(:all, :select=\>‘id’) returns an array of models with only the  
> > ids loaded
> > 
> > model.find(:all, :select=\>‘id’).collect(&:id) returns an array of ids
> > 
> > if you like using the rest of the find options to help generate your sql  
> > for you.
> > 
> > - donald
> 
> Sure, it does. But it creates an Object for each model, while raw SQL  
> gives you an Array of ids. From a performance perspective, the second  
> method is much better.

Thanks for the option donald, I hadn’t seen :select before.

I think I will go with the raw SQL option as this is an area of my  
application that will be under load.

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 12, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/6 "2007-04-12T22:49:19Z")

</div>

> You can do custom sql, or use the :select option to limit the columns  
> returned:
> 
> model.find(:all, :select=\>‘id’) returns an array of models with only the  
> ids loaded
> 
> model.find(:all, :select=\>‘id’).collect(&:id) returns an array of ids
> 
> if you like using the rest of the find options to help generate your sql  
> for you.
> 
> - donald

Sure, it does. But it creates an Object for each model, while raw SQL  
gives you an Array of ids. From a performance perspective, the second  
method is much better.

---

<div class="post-metadata">

**Author:** ![giantcranes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/ecd19e/32.png) [@giantcranes](https://www.ruby-forum.com/u/giantcranes)\
**Post date:** [April 13, 2007, 6:12am UTC](https://www.ruby-forum.com/t/model-find-all-return-a-list-of-ids/91858/7 "2007-04-13T06:12:43Z")

</div>

Giant C. wrote:

> bit of sql?
> 
> Thanks,  
> GiantCranes

ids = Model.find(:all, :select =\> “id”)

–  
Michael W.
