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 -- 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-/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 4/12/07, Giant Cranes <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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?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 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks Rick, I''ll do just that then. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Ball, Donald A Jr \(Library\)
2007-Apr-12 20:42 UTC
Re: Model.find(:all) - return a list of ids
> 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 --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> 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. > > - donaldSure, 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. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
>> 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. -- 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-/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 -~----------~----~----~----~------~----~------~--~---
Giant Cranes wrote:> 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 >ids = Model.find(:all, :select => "id") -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---