开发者

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜