Rails Order by model method

63 views Asked by At

How can I use the model method in scope for ordering?

I have to order the users list, users who have failed go in the last.

scope :position_ordered, -> { order('user.finish_position ASC NULLS LAST, users.failed?') }   

def failed?
---
end
1

There are 1 answers

0
kouroubel On

How about using the scope to order based on the finished position and the apply the sort_by on the result? Something like this...

User.position_ordered.sort_by { |a| a.failed? 0 : 1 }