It looks like the priority calculation just manipulates the time_left
such that the incomplete task with the largest time_left has the
highest priority. Why can't you just order by time_left?
Hmmm... the trouble here is that time_left is also a method of the
Task class, rather than a database column.
Alternatively, you could add a priority column to your table and then
use a callback like before_save to ensure the priorities are kept up
to date. That's going to be vastly more efficient when you are
displaying a list of tasks.
This makes a lot of sense, actually - there's a fair few methods of
the task class that dynamically calculate values based on database
values in this way, so caching them in the database would probably be
sensible. The trick in that case is writing them back to the database
in such an order that they are kept up to date (if priority and
time_left are both calculated at run-time, and both need writing back
to the database, but priority is calculated based upon time_left, I
need to make sure that time_left is calculated and written back to the
database first, presumably). Also, would this not have a pretty
significant efficiency cost (with a non-trivial number of tasks) when
saving records, as the priority and time_left of all the database
records would need to be updated en masse whenever one record was
Thanks for your help!