Querying objects in order

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

On 9/11/07, Peter M. [email protected] 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.

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! :slight_smile: