Finding :last

Rails makes it easy to find the first row of a query:

Order.find(:first)

But what if you want the last one? It’d be great to be able to go:

Order.find(:last)

… especially if you could pass in conditions, etc.

Well, this isn’t general purpose, but it tends to get you the last one that was created. Can be useful in testing circumstances:

last_order = Order.find(:first, :conditions => 'id = (select max(id) from orders)')

Leave a Reply