goldmine
2008-Mar-26 08:15 UTC
Help! how can i get the position of the record in a query result
for example I use find method do a query, the result is a collection. is there any convenient way to know the position of each record in this collection? --~--~---------~--~----~------------~-------~--~----~ 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 Bush
2008-Mar-26 08:31 UTC
Re: Help! how can i get the position of the record in a quer
goldmine wrote:> for example I use find method do a query, the result is a collection. > is there any convenient way to know the position of each record in > this collection?The collection behaves like an array (actually like an Enumerable). If you need to iterate over the collection and include the index in the collection of each item, then you can use #each_with_index http://www.ruby-doc.org/core/classes/Enumerable.html#M003168 -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Bush wrote:> goldmine wrote: >> for example I use find method do a query, the result is a collection. >> is there any convenient way to know the position of each record in >> this collection? > > The collection behaves like an array (actually like an Enumerable). If > you need to iterate over the collection and include the index in the > collection of each item, then you can use #each_with_index > > http://www.ruby-doc.org/core/classes/Enumerable.html#M003168Next, the array will be in database-convenient order without an :order=> clause in your find! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
goldmine
2008-Mar-26 09:09 UTC
Re: Help! how can i get the position of the record in a query result
Done, Thanks for your reply! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---