Incorrect SQL generated for .last when ordering uses an SQL function

I bumped into a problem with using COALESCE in sorting but it's relevant to any function like min or max.

When I have an order defined as `COALESCE(t.field1, t.field2)`, all works great until .last is used. ActiveRecord was generating invalid SQL `select whatever from t ORDER BY COALESCE(t.field1 DESC, t.field2 DESC) LIMIT 1`.