Forum: Ruby on Rails ActiveRecord - return array of only ids?

Ff168162d53e22788d576582b3527e97?d=identicon&s=25 Bill Kocik (Guest)
on 2008-02-10 03:07
(Received via mailing list)
Is it possible to get ActiveRecord to run a query and return an array
of only the ids of the rows that matched? It seems to insist on giving
me back an array of objects, even if I specify ":select => 'id'" as
part of my find(). I don't want to have to loop over the objects
getting all the ids if I don't have to. Can't AR be coaxed into
returning ids and only ids?

I've pored over documentation and searched the web and I haven't
really come up with much. Thought I'd try you all.

Thanks,
-Bill
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2008-02-10 03:09
(Received via mailing list)
On Feb 9, 2008 6:06 PM, Bill Kocik <bkocik@gmail.com> wrote:
> really come up with much. Thought I'd try you all.
You'll need to drop back to the raw adapter for a custom query.  #find
is only interested in dealing with ActiveRecord instances.

Foo.connection.select_values "SELECT id FROM foos ..."





--
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
365e685dc1b4370ec5453bd8519f0abd?d=identicon&s=25 brewpoo (Guest)
on 2008-02-10 03:12
(Received via mailing list)
Object.find(:all, :select => 'id') works but I don't think that is
what you really want.

You want [1,2,3,4,5], correct?

If so, you can do Object.find(:all,:select=>'id').map {|x| x.id}
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2008-02-10 15:20
(Received via mailing list)
> Object.find(:all, :select => 'id') works but I don't think that is
> what you really want.
>
> You want [1,2,3,4,5], correct?
>
> If so, you can do Object.find(:all,:select=>'id').map {|x| x.id}

You can alos use symbol to proc trick here:

Object.find(:all,:select=>'id').map(&:id)


Regards,
Rimantas
--
http://rimantas.com/
Ff168162d53e22788d576582b3527e97?d=identicon&s=25 Bill Kocik (Guest)
on 2008-02-10 19:22
(Received via mailing list)
Excellent suggestions, everyone. Thank you!
E4e17e45f2e8cc967a8695131910f365?d=identicon&s=25 Thufir (Guest)
on 2008-02-20 11:51
(Received via mailing list)
On Sat, 09 Feb 2008 18:12:00 -0800, brewpoo wrote:


> If so, you can do Object.find(:all,:select=>'id').map {|x| x.id}

How do I find out about the magic of "map"?


thanks,

Thufir
E163d36afbbcae05d1b6408265fd616d?d=identicon&s=25 Maximiliano Guzman (Guest)
on 2008-02-20 13:22
(Received via mailing list)
F120fbe41b635d495a361eef790ae8c7?d=identicon&s=25 Gregory C. (gregory_c)
on 2012-07-14 14:07
Should anyone else come across this, another way to refine this further
is:

Object.pluck(:id)

This will produce your array of ids.

More here
http://guides.rubyonrails.org/active_record_queryi...
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.