Forum: Ruby on Rails Model.find(:all) - return a list of ids

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-04-12 22:03
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-04-12 22:34
(Received via mailing list)
On 4/12/07, Giant Cranes <rails-mailing-list@andreas-s.net> 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 Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-04-12 22:35
Thanks Rick, I'll do just that then.
E1f43bafda26307a050d11902752b2a6?d=identicon&s=25 Ball, Donald A Jr (Library) (Guest)
on 2007-04-12 22:43
(Received via mailing list)
> 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
D1f1c20467562fc1d8c8aa0d328def62?d=identicon&s=25 Florian Gilcher (skade)
on 2007-04-12 22:49
> 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.
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-04-12 22:55
>> 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.
Dfd6f9989147955682701da8227df8b4?d=identicon&s=25 Michael Wang (Guest)
on 2007-04-13 06:12
(Received via mailing list)
Giant Cranes wrote:
> bit of sql?
>
> Thanks,
> GiantCranes
>

ids = Model.find(:all, :select => "id")


--
Michael Wang
This topic is locked and can not be replied to.