Navin Navin - 2 years ago 112
Ruby Question

rails each order with nil value

In rails application I want to sort task by priority, the priority have the value p1,p2,p3,nil. If I sort like this

task..order(:priority).each do |t|
puts t

The task which have nil as priority comes first, then other things are sorted based on order. I want task which have nil priority to go last on loop. How to accomplish this one?

I am using mysql db

Answer Source
Task.order('priority IS NULL, priority DESC')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download