Max Williams
2008-Dec-04 12:24 UTC
Get a collection using an array of ids, keeping the order
Say if i have an array of resource ids, of size n. Is there a way to get all the corresponding resources, without doing n individual Find calls? eg, i could do this: @resources = [] array.each{|id| @resources << Resource.find(id)} But, this requires n Find calls which isn''t acceptable. Any ideas anyone? thanks max -- 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-04 12:31 UTC
Re: Get a collection using an array of ids, keeping the order
On 4 Dec 2008, at 12:24, Max Williams wrote:> > Say if i have an array of resource ids, of size n. Is there a way to > get all the corresponding resources, without doing n individual Find > calls? > > eg, i could do this: > > @resources = [] > array.each{|id| @resources << Resource.find(id)} > > But, this requires n Find calls which isn''t acceptable.Resource.find @resources Fred> > > Any ideas anyone? > > thanks > max > -- > 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 -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-04 12:36 UTC
Re: Get a collection using an array of ids, keeping the order
On 4 Dec 2008, at 12:31, Frederick Cheung wrote:> > On 4 Dec 2008, at 12:24, Max Williams wrote: > >> >> Say if i have an array of resource ids, of size n. Is there a way to >> get all the corresponding resources, without doing n individual Find >> calls? >> >> eg, i could do this: >> >> @resources = [] >> array.each{|id| @resources << Resource.find(id)} >> >> But, this requires n Find calls which isn''t acceptable. > > Resource.find @resources >Sorry, only read the body of the message, not the subject! I would sort it after, eg order_hash = {} array.each_with_index {|id, index| order_hash[id]=index Resource.find(array).sort_by {|r| order_hash[r.id]} Fred> Fred >> >> >> Any ideas anyone? >> >> thanks >> max >> -- 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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2008-Dec-04 12:46 UTC
Re: Get a collection using an array of ids, keeping the order
Frederick Cheung wrote:> On 4 Dec 2008, at 12:31, Frederick Cheung wrote: > >>> @resources = [] >>> array.each{|id| @resources << Resource.find(id)} >>> >>> But, this requires n Find calls which isn''t acceptable. >> >> Resource.find @resources >> > Sorry, only read the body of the message, not the subject! > I would sort it after, eg > > order_hash = {} > array.each_with_index {|id, index| order_hash[id]=index > Resource.find(array).sort_by {|r| order_hash[r.id]} > > Fredah yes, of course. Nice touch with the hash (as opposed to sorting by array.index) :) Thanks a lot, Fred. -- 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 -~----------~----~----~----~------~----~------~--~---
Rob Biedenharn
2008-Dec-04 13:59 UTC
Re: Get a collection using an array of ids, keeping the order
On Dec 4, 2008, at 7:46 AM, Max Williams wrote:> Frederick Cheung wrote: >> On 4 Dec 2008, at 12:31, Frederick Cheung wrote: >> >>>> @resources = [] >>>> array.each{|id| @resources << Resource.find(id)} >>>> >>>> But, this requires n Find calls which isn''t acceptable. >>> >>> Resource.find @resources >>> >> Sorry, only read the body of the message, not the subject! >> I would sort it after, eg >> >> order_hash = {} >> array.each_with_index {|id, index| order_hash[id]=index >> Resource.find(array).sort_by {|r| order_hash[r.id]} >> >> Fred > > ah yes, of course. Nice touch with the hash (as opposed to sorting by > array.index) :) > > Thanks a lot, Fred.YMMV, but why bother with the hash? Unless you have a truly huge list of id''s @resources = Resource.find(array).sort_by {|r| array.index(r.id)} You could even hide the implementation: class Resource def self.find_by_ordered_ids(*array) options = {} options = array.pop if array.last.is_a? Hash # pass an Array or a list of id arguments array = array.flatten if array.first.is_a? Array find(array, options).sort_by {|r| array.index(r.id)} end end (Note, untested, but that doesn''t mean that it *won''t* work ;-) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2008-Dec-05 00:56 UTC
Re: Get a collection using an array of ids, keeping the order
If you use MySQL, the "field" function allows you to retrieve records in a specific id order: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1 -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.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 -~----------~----~----~----~------~----~------~--~---
Max Williams
2008-Dec-05 09:23 UTC
Re: Get a collection using an array of ids, keeping the order
Mark Reginald James wrote:> If you use MySQL, the "field" function allows you > to retrieve records in a specific id order: > > http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/158dc2d879b2fb1/af78ce75ddfa1ed1 > > -- > Rails Wheels - Find Plugins, List & Sell Plugins - > http://railswheels.comAh, now that is a great tip, i''d not seen this before! Thanks a lot. The weird thing is, i''ve gone to my console to try it out, and rails is now always giving me the resources back in the order of the id array anyway, whether i use the field option or not. It''s the same with any of my AR classes! I''m sure that this didn''t used to happen though...i think i''m going crazy. Can anyone tell me if this is normal? If so then i just imagined this whole problem... AModel.find([3,1,2]).collect(&:id) => [3,1,2] or do you get => [1,2,3] ? -- 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 -~----------~----~----~----~------~----~------~--~---
Robert Head
2010-Aug-23 02:18 UTC
Re: Get a collection using an array of ids, keeping the order
Max Williams wrote:> The weird thing is, i''ve gone to my console to try it out, and rails is > now always giving me the resources back in the order of the id array > anyway, whether i use the field option or not. It''s the same with any > of my AR classes! I''m sure that this didn''t used to happen though...i > think i''m going crazy. Can anyone tell me if this is normal?You are not going crazy. MySQL may, but is not required to, return the objects in the order of the array. So, the behavior will change without warning. You still need to sort after the fetch. -- 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.
Michael Pavling
2010-Aug-23 07:38 UTC
Re: Re: Get a collection using an array of ids, keeping the order
On 23 August 2010 03:18, Robert Head <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Max Williams wrote: >> i think i''m going crazy. Can anyone tell me if this is normal? > > You are not going crazy.I am! This is a reply to a two-year-old thread... I just wasted 10 minutes trying to find out why I hadn''t received the previous messages. I thought my email spam filter was being paranoid :-/ -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.