'Rails Order by model method
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
Solution 1:[1]
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 }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |