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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 9, 2008 6:06 PM, Bill Kocik <bkocik-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > 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.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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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} On Feb 9, 9:06 pm, Bill Kocik <bko...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Excellent suggestions, everyone. Thank you! On Feb 10, 9:08 am, "Rimantas Liubertas" <riman...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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/--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
http://www.ruby-doc.org/core/classes/Enumerable.html#M003159 http://tech.rufy.com/2006/11/functional-programming-in-ruby.html On Feb 20, 2008 8:50 AM, Thufir <hawat.thufir-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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_querying.html#pluck -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en-US.