# Help! how can i get the position of the record in a query result

**URL:** <https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968>\
**Category:** Rails\
**Created:** [March 26, 2008, 9:16am UTC](https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968 "2008-03-26T09:16:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![goldmine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@goldmine](https://www.ruby-forum.com/u/goldmine)\
**Post date:** [March 26, 2008, 9:16am UTC](https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968/1 "2008-03-26T09:16:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![goldmine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@goldmine](https://www.ruby-forum.com/u/goldmine)\
**Post date:** [March 26, 2008, 9:31am UTC](https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968/2 "2008-03-26T09:31:47Z")

</div>

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](http://www.ruby-doc.org/core/classes/Enumerable.html#M003168)

---

<div class="post-metadata">

**Author:** ![goldmine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@goldmine](https://www.ruby-forum.com/u/goldmine)\
**Post date:** [March 26, 2008, 9:36am UTC](https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968/3 "2008-03-26T09:36:28Z")

</div>

Mark B. 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
> 
> [module Enumerable - RDoc Documentation](http://www.ruby-doc.org/core/classes/Enumerable.html#M003168)

Next, the array will be in database-convenient order without an :order=\>  
clause  
in your find!

---

<div class="post-metadata">

**Author:** ![goldmine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4bbf92/32.png) [@goldmine](https://www.ruby-forum.com/u/goldmine)\
**Post date:** [March 26, 2008, 10:10am UTC](https://www.ruby-forum.com/t/help-how-can-i-get-the-position-of-the-record-in-a-query-result/132968/4 "2008-03-26T10:10:15Z")

</div>

Done, Thanks for your reply!
