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?

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

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

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!

Done, Thanks for your reply!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs