views:

21

answers:

3

I want to sort by two columns, one is a DateTime (updated_at), and the other is a Decimal (Price)

I would like to be able to sort first by updated_at, then, if multiple items occur on the same day, sort by Price.

Ideas?

A: 
Model.all(:order => 'updated_at, price')
robertokl
A: 
Thing.find(:all, :order => "updated_at desc, price asc")

will do the trick.

Erik Escobedo
+2  A: 

Assuming you're using MySQL,

Model.all(:order => 'DATE(updated_at), price')

Note the distinction from the other answers. The updated_at column will be a full timestamp, so if you want to sort based on the day it was updated, you need to use a function to get just the date part from the timestamp. In MySQL, that is DATE().

Daniel Vandersluis