Rails weird association sorting
I have two models in a habtm association (Round and User) I want to get all the users and sort the ones that belong to a certain round (current_round) to the top. I don't want to di开发者_如何学Goscard the other users though, they all need to be in the resulting array.
Since User belongs to Round, you can try Users LEFT JOIN Rounds like this:
User.all(:joins => "LEFT JOIN rounds ON user.round_id = rounds.id", :order => "rounds.id DESC")
That should pull all the users that belongs to a certain round plus all the users that do not belong to any round at all which will be at the bottom or top depending on how you order rounds.id (ASC or DESC).
精彩评论