# Querying objects in order

**URL:** <https://www.ruby-forum.com/t/querying-objects-in-order/114386>\
**Category:** Rails\
**Created:** [September 12, 2007, 1:54am UTC](https://www.ruby-forum.com/t/querying-objects-in-order/114386 "2007-09-12T01:54:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 12, 2007, 1:54am UTC](https://www.ruby-forum.com/t/querying-objects-in-order/114386/1 "2007-09-12T01:54:00Z")

</div>

I know how do find the first object that matches a set of conditions:

Model.find(:first, :conditions =\> [“id = ?”, id])

How could I find the second and third object and so on?

Thanks in advance,

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 12, 2007, 4:14am UTC](https://www.ruby-forum.com/t/querying-objects-in-order/114386/2 "2007-09-12T04:14:57Z")

</div>

On 9/11/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know how do find the first object that matches a set of conditions:
> 
> Model.find(:first, :conditions =\> [“id = ?”, id])
> 
> How could I find the second and third object and so on?

(id=? might not be a good condition, since there will only be one row  
per id)

You could use find(:all) and then step through them.

If you want to find them one at a time, use :offset

Model.find :first, :conditions =\> blah, :offset =\> 0 # first  
Model.find :first, :conditions =\> blah, :offset =\> 1 # second  
Model.find :first, :conditions =\> blah, :offset =\> 2 # third

It’s basically paginating with a page size of 1

I don’t know if this is guaranteed to work for all databases. It works  
for MySQL. Also you probably should use :order whenever you use  
:offset.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 12, 2007, 7:35am UTC](https://www.ruby-forum.com/t/querying-objects-in-order/114386/3 "2007-09-12T07:35:26Z")

</div>

> Model.find :first, :conditions =\> blah, :offset =\> 0 # first  
> Model.find :first, :conditions =\> blah, :offset =\> 1 # second  
> Model.find :first, :conditions =\> blah, :offset =\> 2 # third

You’re right about the id condition, I was just using as an example.  
:offset is exactly what I was looking for though. Thanks a bunch Bob! 🙂
